Advertisement
muriela

Untitled

May 22nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. """You are given a text, which contains different english letters and punctuation symbols.
  2. You should find the most frequent letter in the text. The letter returned must be in lower case.
  3. While checking for the most wanted letter, casing does not matter, so for the purpose of your search, "A" == "a".
  4. Make sure you do not count punctuation symbols, digits and whitespaces, only letters.
  5.  
  6. If you have two or more letters with the same frequency, then return the letter which comes first in the latin alphabet.
  7. For example -- "one" contains "o", "n", "e" only once for each, thus we choose "e".
  8.  
  9. Input: A text for analysis as a string.
  10.  
  11. Output: The most frequent letter in lower case as a string.
  12.  
  13. Precondition:
  14. A text contains only ASCII symbols.
  15. 0 < len(text) ≤ 105"""
  16.  
  17.  
  18. def checkio(text):
  19. letters = []
  20. dict = {}
  21. # dict_max = {}
  22. letter_max = 0
  23.  
  24. for letter in text:
  25. if letter.isalpha():
  26. letters.append(letter.lower())
  27.  
  28. for l in letters:
  29. letter_is_in_dict = dict.get(l)
  30. if not letter_is_in_dict:
  31. dict[l] = 1
  32. else:
  33. dict[l] += 1
  34.  
  35. list_of_letters = dict.items()
  36.  
  37. for i in list_of_letters:
  38. if i[1] >= letter_max:
  39. letter_max = i[1]
  40. list_of_letters = i
  41. else:
  42. pass
  43.  
  44. print(list_of_letters)
  45.  
  46. # sorted_list = sorted(dict.items(), key=lambda x: x[1])
  47. #
  48. # max_letter = [()]
  49. #
  50. # for tuple in sorted_list:
  51. # if tuple[]
  52.  
  53.  
  54. # print(1)
  55.  
  56.  
  57. if __name__ == '__main__':
  58. checkio("Hello World!")
  59. #These "asserts" using only for self-checking and not necessary for auto-testing
  60. # assert checkio("Hello World!") == "l", "Hello test"
  61. # assert checkio("How do you do?") == "o", "O is most wanted"
  62. # assert checkio("One") == "e", "All letter only once."
  63. # assert checkio("Oops!") == "o", "Don't forget about lower case."
  64. # assert checkio("AAaooo!!!!") == "a", "Only letters."
  65. # assert checkio("abe") == "a", "The First."
  66. # print("Start the long test")
  67. # assert checkio("a" * 9000 + "b" * 1000) == "a", "Long."
  68. # print("The local tests are done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement