Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. """Order names by popularity."""
  2.  
  3.  
  4. def read_from_file() -> list:
  5. """
  6. Create the list of all the names.
  7.  
  8. :return: list
  9. """
  10. names = []
  11. with open("popular_names.txt", encoding='utf-8') as file:
  12. for line in file:
  13. names.append(line.strip())
  14. return names
  15.  
  16.  
  17. def to_dictionary(names: list) -> dict:
  18. """
  19. Make a dictionary from a list of names.
  20.  
  21. :param names: list of all the names
  22. :return: dictionary {"name:sex": number}
  23. """
  24. names_dict = {}
  25. for name in names:
  26. if name in names_dict:
  27. names_dict[name] += 1
  28. else:
  29. names_dict[name] = 1
  30. return names_dict
  31.  
  32.  
  33. def to_sex_dicts(names_dict: dict) -> tuple:
  34. """
  35. Divide the names by sex to 2 different dictionaries.
  36.  
  37. :param names_dict: dictionary of names
  38. :return: two dictionaries {"name": number}, {"name": number}
  39. first one is male names, seconds is female names.
  40. """
  41. male_dict = {}
  42. female_dict = {}
  43.  
  44. for key in names_dict:
  45. if key[-1:] == "F":
  46. female_dict[key[:-2]] = names_dict[key]
  47. elif key[-1:] == "M":
  48. male_dict[key[:-2]] = names_dict[key]
  49. return male_dict, female_dict
  50.  
  51.  
  52. def most_popular(names_dict: dict) -> str:
  53. """
  54. Find the most popular name in the dictionary.
  55.  
  56. If the dictionary is empty, return "Empty dictionary."
  57. :param names_dict: dictionary of names
  58. :return: string
  59. """
  60. if len(names_dict) == 0:
  61. return "Empty dictionary."
  62. else:
  63. return max(names_dict, key=names_dict.get)
  64.  
  65.  
  66. def number_of_people(names_dict: dict) -> int:
  67. """
  68. Calculate the number of people in the dictionary.
  69.  
  70. :param names_dict: dictionary of names
  71. :return: int
  72. """
  73. return sum(names_dict.values())
  74.  
  75.  
  76. def names_by_popularity(names_dict: dict) -> str:
  77. r"""
  78. Create a string used to print the names by popularity.
  79.  
  80. Format:
  81. 1. name: number of people + "\n"
  82. ...
  83.  
  84. Example:
  85. 1. Kati: 100
  86. 2. Mati: 90
  87. 3. Nati: 80
  88. ...
  89.  
  90. :param names_dict: dictionary of the names
  91. :return: string
  92. """
  93. new_line = "\n"
  94. i = 0
  95. solution_list = []
  96.  
  97. for key in list(names_dict):
  98. i = i + 1
  99. solution_list.append(f"{str(i)}. {most_popular(names_dict)}: {max(names_dict.values())}{new_line}"), print(key)
  100. names_dict.pop(most_popular(names_dict))
  101.  
  102. final_answer = ''.join(solution_list)
  103. return final_answer
  104.  
  105.  
  106. if __name__ == '__main__':
  107. example_names = ("Kati:F\n" * 1000 + "Mati:M\n" * 800 + "Mari:F\n" * 600 + "Tõnu:M\n" * 400).rstrip("\n").split("\n")
  108. people = to_dictionary(example_names)
  109. print(people) # -> {'Kati:F': 1000, 'Mati:M': 800, 'Mari:F': 600, 'Tõnu:M': 400}
  110. male_names, female_names = to_sex_dicts(people)
  111. print(male_names) # -> {'Mati': 800, 'Tõnu': 400}
  112. print(female_names) # -> {'Kati': 1000, 'Mari': 600}
  113. print(most_popular(male_names)) # -> "Mati"
  114. print(number_of_people(people)) # -> 2800
  115. print(names_by_popularity(male_names)) # -> 1. Mati: 800
  116. # 2. Tõnu: 400
  117. # (empty line)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement