Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """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.
- Precondition:
- A text contains only ASCII symbols.
- 0 < len(text) ≤ 105"""
- def checkio(text):
- letters = []
- dict = {}
- # dict_max = {}
- letter_max = 0
- for letter in text:
- if letter.isalpha():
- letters.append(letter.lower())
- for l in letters:
- letter_is_in_dict = dict.get(l)
- if not letter_is_in_dict:
- dict[l] = 1
- else:
- dict[l] += 1
- list_of_letters = dict.items()
- for i in list_of_letters:
- if i[1] >= letter_max:
- letter_max = i[1]
- list_of_letters = i
- else:
- pass
- print(list_of_letters)
- # sorted_list = sorted(dict.items(), key=lambda x: x[1])
- #
- # max_letter = [()]
- #
- # for tuple in sorted_list:
- # if tuple[]
- # print(1)
- if __name__ == '__main__':
- checkio("Hello World!")
- #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.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement