Check python function determine isogram from codewars

Question:

An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.

is_isogram("Dermatoglyphics" ) == true
is_isogram("aba" ) == false
is_isogram("moOse" ) == false # -- ignore letter case

Here is my code:

def is_isogram(string):
    string = string.lower()
    for char in string:
        if string.count(char) > 1:
            return False
        else:
            return True

And when I tried to run the test code Test.assert_equals(is_isogram("moOse"), False, "same chars may not be same case" ) It failed, but I thought I did convert everything into lowercase. Can someone help?

Asked By: Loool Hoop

||

Answers:

Try this:

def is_isogram(string):
    string = string.lower()
    for char in string:
        if string.count(char) > 1:
            return False
    return True

In your code when is_isogram("moose") is called, it will see that the first character’s (‘m’) count is not greater than 1. So it will return True. Once it hits the return statement, it will stop the execution for the rest string. So you should really write return True only after for-loop to make sure that the function checks for the whole string.

If however, at any point, it finds a character’s count to be greater than 1, then it will simply return False and stop executing because there’s no point of checking any more when one point is found where condition does not hold.

Answered By: Shubham

How about using sets? Casting the string into a set will drop the duplicate characters, causing isograms to return as True, as the length of the set won’t differ from the length of the original string:

def is_isogram(s):
    s = s.lower()
    return len(set(s)) == len(s)

print is_isogram("Dermatoglyphics")
print is_isogram("aba")
print is_isogram("moOse")
print is_isogram("")

This outputs:

True
False
False
True
Answered By: Bahrom

let us define an isogram well:
according to wikipedia An Isogram is a word in which no letter occurs more than once.
check here for more about an isogram

just remind letter
I write this code and it works for me :

def is_isogram(argument):
   print(len(argument))
   if isinstance(argument,str):
      valeur=argument.lower()
      if not argument:
         return False
      else:
         for char in valeur:
            if valeur.count(char)>1 or not char.isalpha():
               return False
         return True
   else:
      raise TypeError("need a string ")

NB: the hidden test is the fact that you must check if the char in the string is a alpha character a-z, when i add this it pass all the hiddens tests
up vote if this help

Answered By: Espoir Murhabazi

Try this :

def is_isogram(s):
    string = s.lower()
    if len(s) == len(set(string)):
        return True
    return False
Answered By: ANONIM

Try this out:

def is_isogram(string):
    return len(string) == len(set(string.lower()))

"Implement a function that determines whether a string that contains only letters is an isogram."

By using sets, you can create unique elements. So if there are any repeating numbers, it will only select one. By calling len() on these strings, you can compare the length to the original.

Sorry if I explained it poorly. I am working on this.

Answered By: tommyngu10

I reckon this might not be the best solution in terms of maximizing memory space and time. This answer is just for intuition purposes using a dictionary and two for loops:

def is_isogram(string):
    #your code here
    #create an empty dictionary
    m={}
    #loop through the string and check for repeating characters
    for char in string:
      #make all characters lower case to ignore case variations
       char = char.lower()
       if char in m:
            m[char] += 1
        else:
            m[char] = 1
   #loop through dictionary and get value counts.
    for j, v in m.items():
       #if there is a letter/character with a count > 1 return False
        if v > 1:
            return False
    #Notice the scope of the "return True" command. It is outside.   
    return True 
Answered By: Alex Maina