Loading

Isograms

Details
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.
Example

 "Dermatoglyphics" = true
 "moose" = false
 "aba" = false

Solutions
mine

def is_isogram(string):
    if string:
        for index in range(len(string)):
            index_two = index + 1
            while index != len(string) - 1:
                if string[index].lower() == string[index_two].lower():
                    return False
                elif index_two <= len(string) - 2:
                    index_two += 1
                    continue

                break

    return True

Best:

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

set()函数是Python的内置函数之一,用于创建一个无序不重复元素集

posted @ 2023-04-25 18:42  Artwalker  阅读(5)  评论(0编辑  收藏  举报
Live2D