Home > database >  Function to see if word is isogram
Function to see if word is isogram

Time:05-13

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.

I have the following as the furthest I can make it. But the goal is trying to say that 'moOse' is not an isogram. It says to not worry about casing. I am not sure how to differentiate between 'o' and 'O'.

    char_list = []
    for char in string:
        if char.isalpha():
            if char.islower():
                if char in char_list:
                    return False
                else:
                    char_list.append(char)
    return True

My script returns true because it sees the 'o' and 'O' differently. How do I get it to not differentiate? I tried adding .islower(), but it doesn't seem to work.

CodePudding user response:

Short and straightforward solution:

def check_isogram(text):
 return len(set(text.lower())) == len(text)

CodePudding user response:

You can just uppercase the string before checking whether it contains duplicate letters:

def is_isogram(string):
    char_list = []
    for char in string.upper():
        if char in char_list:
            return False
        char_list.append(char)
    return True

You can make this faster by using a set rather than a list:

def is_isogram(string):
    char_list = set()
    for char in string.upper():
        if char in char_list:
            return False
        char_list.add(char)
    return True

And you can make this even more concise by using the len() function:

def is_isogram(string):
    return len(string) == len(set(string.upper()))
  • Related