python-checkio/Polygon/most-wanted-letter-2.py

28 lines
1.1 KiB
Python
Raw Normal View History

2020-01-08 17:32:09 +01:00
def most_wanted(text: str) -> str:
text = text.lower()
letters = []
max = 0
for letter in text:
if letter.isalpha():
if letter not in letters:
count = text.count(letter)
if max == count:
letters.append(letter)
if count > max:
letters = [letter]
max = count
return letters
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert sorted(most_wanted("Hello World!")) == ["l"], "Hello test"
assert sorted(most_wanted("How do you do?")) == ["o"], "O is most wanted"
assert sorted(most_wanted("One")) == ["e", "n", "o"], "All letter only once."
assert sorted(most_wanted("Oops!")) == ["o"], "Don't forget about lower case."
assert sorted(most_wanted("AAaooo!!!!")) == ["a", "o"], "Only letters."
assert sorted(most_wanted("abe")) == ["a", "b", "e"], "The First."
print("Start the long test")
assert sorted(most_wanted("a" * 9000 + "b" * 1000)) == ["a"], "Long."
print("The local tests are done.")