Python Forum
writing a function for isogram
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing a function for isogram
#1
I need to write 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'

This is the code i created


def is_isogram(word):
    if type(word) == str:
      for i in word:
        if word.count(i) >1 or word == "":
          return (word, False)
        else:
          return (word, True)
    else:
      raise TypeError ("'{}' should be a string" .format(word))
And this is the test code


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'
      )
The code runs well but when I want to test it against the test code. It gives me the error code
NameError Global name is_isogram not defined.

kindly help
Reply
#2
is_isogram() should be defined in the same source file, or you should "import" the source file that contains it.
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
#3
One note:
isogram is also used by some to mean a word or phrase in which each letter appears the same number of times, not necessarily just once.
Reply
#4
Also, your is_isogram function won't work right. You should only return true if none of the letters returns false.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
1) import the file. That'll solve this issue, but there are others you'll run into once you do that.

2) Why are you type checking? And why are you checking specifically against str? Why not allow any iterable of items that support __eq__? By limiting the function to just strings, you're eliminating a lot of the benefit that python's typing brings to the table.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,797 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  is writing a function a pythonic thing to do? Avivlevi815 1 2,108 Dec-03-2018, 06:10 PM
Last Post: Gribouillis
  Writing python function difficulty kirito85 5 3,253 Oct-28-2018, 07:34 AM
Last Post: buran
  Writing a function that accepts two integer parameters (lines and cheers) taydeal20 1 3,091 Feb-05-2018, 08:35 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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