输出字符串中出现次数最多的字符

题目:输出给定字符串中出现次数最多 的字符(不论大小写),字符串中可能存在大小写,当存在出现次数同样的字符,此时按照字符在字母表中的顺序输出位于字母表前面的字符。this

import string
def checkio(text):
    alpha=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    #replace this for solution
    #最简洁的解决代码为一下注释的三行便可
    #text=text.lower()
    #alpha=string.ascii_lowercase
    #return max(alpha,key=text.count)

    max_str=[0,0]
    for i in range(26):
        if text.lower().count(alpha[i]) > max_str[1] :
             max_str[0]=alpha[i]
             max_str[1]=text.lower().count(alpha[i])
    return max_str[0]


if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio("Hello World!") == "l", "Hello test"
    assert checkio("How do you do?") == "o", "O is most wanted"
    assert checkio("One") == "e", "All letter only once."
    assert checkio("Oops!") == "o", "Don't forget about lower case."
    assert checkio("AAaooo!!!!") == "a", "Only letters."
    assert checkio("abe") == "a", "The First."
    print("Start the long test")
    assert checkio("a" * 9000 + "b" * 1000) == "a", "Long."
    print("The local tests are done.")
相关文章
相关标签/搜索