Python Forum
Solutions to this questions
Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Solutions to this questions
#1
QUESTION 1.

please help me

I have two solutions to the task and they are all passing the visible tests but when it is time to submit, I keep getting my failed to pass all tests. There are still some hidden tests my code isn't passing.

The task reads as follow:

Write a program that checks if a word supplied as the argument is an Isogram. An Isogram is a word in which no letter occurs more than once.

Create a method called is_isogram that takes one argument, a word to test if it's an isogram. This method should return a tuple of the word and a boolean indicating whether it is an isogram.

If the argument supplied is an empty string, return the argument and False: (argument, False). If the argument supplied is not a string, raise a TypeError with the message 'Argument should be a string'.

Example:

is_isogram("abolishment")

Expected result:

("abolishment", True)

The visible tests:
from unittest import TestCase

class IsogramTestCases(TestCase):
  def test_checks_for_isograms(self):
    word = 'abolishment'
    self.assertEqual(
      is_isogram(word),
      (word, True),
      msg="Isogram word, '{}' not detected correctly".format(word)
    )

  def test_returns_false_for_nonisograms(self):
    word = 'alphabet'
    self.assertEqual(
      is_isogram(word),
      (word, False),
      msg="Non isogram word, '{}' falsely detected".format(word)
    )

  def test_it_only_accepts_strings(self):
    with self.assertRaises(TypeError) as context:
      is_isogram(2)
      self.assertEqual(
        'Argument should be a string',
        context.exception.message,
        'String inputs allowed only'
      )
I wrote two different solutions to the task and they both passed the tests without errors but when I try submitting I got "Test Spec Failed Your solution failed to pass all the tests".

Please any elegant way of solving this task?


Question 2. 
       write a function my_sort which takes in a list of numbers (positive integers) the function should return a list of sorted numbers such that odd numbers come first and even last..... Thank u
Reply
#2
QUESTION 1.

please help me

I have two solutions to the task and they are all passing the visible tests but when it is time to submit, I keep getting my failed to pass all tests. There are still some hidden tests my code isn't passing.

The task reads as follow:

Write a program that checks if a word supplied as the argument is an Isogram. An Isogram is a word in which no letter occurs more than once.

Create a method called is_isogram that takes one argument, a word to test if it's an isogram. This method should return a tuple of the word and a boolean indicating whether it is an isogram.

If the argument supplied is an empty string, return the argument and False: (argument, False). If the argument supplied is not a string, raise a TypeError with the message 'Argument should be a string'.

Example:


is_isogram("abolishment")

Expected result:

("abolishment", True)

The visible tests:
from unittest import TestCase

class IsogramTestCases(TestCase):
 def test_checks_for_isograms(self):
   word = 'abolishment'
   self.assertEqual(
     is_isogram(word),
     (word, True),
     msg="Isogram word, '{}' not detected correctly".format(word)
   )

 def test_returns_false_for_nonisograms(self):
   word = 'alphabet'
   self.assertEqual(
     is_isogram(word),
     (word, False),
     msg="Non isogram word, '{}' falsely detected".format(word)
   )

 def test_it_only_accepts_strings(self):
   with self.assertRaises(TypeError) as context:
     is_isogram(2)
     self.assertEqual(
       'Argument should be a string',
       context.exception.message,
       'String inputs allowed only'
     )
I wrote two different solutions to the task and they both passed the tests without errors but when I try submitting I got "Test Spec Failed Your solution failed to pass all the tests".

Please any elegant way of solving this task?


Question 2.
      write a function my_sort which takes in a list of numbers (positive integers) the function should return a list of sorted numbers such that odd numbers come first and even last..... Thank u

Moderator:
Do NOT post the same thread in multiple locations.  DO enclose any code within the 'python' brackets and any errors within in the 'error' brackets.
Reply
#3
This should go to Homework section of the forums.
I don't see your implementation of "is_isogram()" method, so I can't check for potential mistakes in code. And I have even less clue on how grader works with what you pass to it.
As for question 2, I also don't see your attempt on "my_sort()" implementation, so I can't give any hints for solving it.

Moderator:
Thread moved, thank you for the catch.
Reply
#4
1) Better show us your code than the tests.

2) The sort and sorted functions in Python take a named key argument which is a function (often implemented as a lambda) that takes a list element and returns the element used for the sort (for instance, when sorting people, it would return familyname+firstname). In you case you just need to have a function that returns some constant value for odd numbers and a greater constant value for even ones (for instance 0 and 1). If odd and even numbers have to be sorted by value, sort the whole list first with the "natural" order.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
There is a cross post:
https://python-forum.io/Thread-Solutions...ions--2160
And neither in Homework, where I believe it should be.
Reply
#6
for the question 1.
 here is my solution
def is_isogram(word):
    if type(word) != str:
        raise TypeError('Argument should be a string')

    elif word == " ":
      return word, False
    else:
        word = word.lower()
        for letter in word:
            if word.count(letter) > 1:
                return word, False

                return word, True
print is_isogram("abolishment")
Reply
#7
Maybe the indentation of the second-to-last row may cause problems?
Reply
#8
You should use python code tags to make your code more readable and to keep an indentation right (your third return statement is clearly misindented).

Little problem with your solution is that an empty string is not a string consisting of single space. Evaluation of an empty string is not covered with visible tests, so it is not catched. With that corrected it should work.

You can use len(x) == len(set(x)) for shorter, but maybe less readable check.
Reply
#9
2) You can use Ofnuts' suggestion or do it with just one sorting pass with "composite" key by using something like x -> (0, x) if x is odd, (1, x) otherwise. Either way should work and in either case you should stop and think why it works.

And as was mentioned above, there is crosspost and both threads are in wrong sections. And your choice of thread title is only slightly better than "HELP! I need help!!!!".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compute complex solutions in quadratic equations liam 1 1,849 Feb-09-2020, 04:18 PM
Last Post: Gribouillis
  More Errors. Less Solutions. SyntaxError123 7 4,795 Mar-30-2017, 07:55 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020