【Codewars python 7kyu】: Isograms

问题描述:

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

代码实现:

#codewars第二题
def is_isogram(string):
    #your code here
    if string == "": return True
    string = string.upper()
    _length = len(string)
    for i in range(0,_length):
        for j in range(_length-1,i,-1): #小心下标越界 string[j]中的j不可以取到_length
            if string[i] == string[j]:
                return False
            else: continue
    if i == _length-1:
        return True
    else:
        return False
#其他解法
def is_isogram(s):
  s = s.lower()
  return len(s) < 2 or len([x for i, x in enumerate(s) if s.index(x) == i]) == len(s)

is_isogram("")

 

你可能感兴趣的:(Codewars,python,codewars,7kyu,Isograms)