취미생활/CheckIO

[checkIO]Home 03. The Most Wanted Letter

우주먼지의하루 2020. 2. 9. 13:52
728x90

You are given a text, which contains different english letters and punctuation symbols. You should find the most frequent letter in the text. The letter returned must be in lower case.

While checking for the most wanted letter, casing does not matter, so for the purpose of your search, "A" == "a". Make sure you do not count punctuation symbols, digits and whitespaces, only letters.

If you have two or more letters with the same frequency, then return the letter which comes first in the latin alphabet. For example -- "one" contains "o", "n", "e" only once for each, thus we choose "e".

Input: A text for analysis as a string.

Output: The most frequent letter in lower case as a string.

Example:

checkio("Hello World!") == "l"

checkio("How do you do?") == "o"

checkio("One") == "e"

checkio("Oops!") == "o"

checkio("AAaooo!!!!") == "a"

checkio("abe") == "a"

* 가장 많은 문자를 출력한다.

* 소문자로 출력해야한다.

* 가장 많은 문자가 두개 이상이면, 알파벳 순서 중 가장 빠른 것으로 출력한다.

 

-풀이

 

def checkio(text: str) -> str:
    total = 0
    lower = text.lower()
    
    for i in lower:
        if i.isalpha():
            if lower.count(i) > total:
                most = i
                total = lower.count(i)
            elif lower.count(i) == total:
                if i < most: #sorting
                    most = i
                    total = lower.count(i)
    return most
반응형