Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Order names by popularity."""
- def read_from_file() -> list:
- """
- Create the list of all the names.
- :return: list
- """
- names = []
- with open("popular_names.txt", encoding='utf-8') as file:
- for line in file:
- names.append(line.strip())
- return names
- def to_dictionary(names: list) -> dict:
- """
- Make a dictionary from a list of names.
- :param names: list of all the names
- :return: dictionary {"name:sex": number}
- """
- names_dict = {}
- for name in names:
- if name in names_dict:
- names_dict[name] += 1
- else:
- names_dict[name] = 1
- return names_dict
- def to_sex_dicts(names_dict: dict) -> tuple:
- """
- Divide the names by sex to 2 different dictionaries.
- :param names_dict: dictionary of names
- :return: two dictionaries {"name": number}, {"name": number}
- first one is male names, seconds is female names.
- """
- male_dict = {}
- female_dict = {}
- for key in names_dict:
- if key[-1:] == "F":
- female_dict[key[:-2]] = names_dict[key]
- elif key[-1:] == "M":
- male_dict[key[:-2]] = names_dict[key]
- return male_dict, female_dict
- def most_popular(names_dict: dict) -> str:
- """
- Find the most popular name in the dictionary.
- If the dictionary is empty, return "Empty dictionary."
- :param names_dict: dictionary of names
- :return: string
- """
- if len(names_dict) == 0:
- return "Empty dictionary."
- else:
- return max(names_dict, key=names_dict.get)
- def number_of_people(names_dict: dict) -> int:
- """
- Calculate the number of people in the dictionary.
- :param names_dict: dictionary of names
- :return: int
- """
- return sum(names_dict.values())
- def names_by_popularity(names_dict: dict) -> str:
- r"""
- Create a string used to print the names by popularity.
- Format:
- 1. name: number of people + "\n"
- ...
- Example:
- 1. Kati: 100
- 2. Mati: 90
- 3. Nati: 80
- ...
- :param names_dict: dictionary of the names
- :return: string
- """
- new_line = "\n"
- i = 0
- solution_list = []
- for key in list(names_dict):
- i = i + 1
- solution_list.append(f"{str(i)}. {most_popular(names_dict)}: {max(names_dict.values())}{new_line}"), print(key)
- names_dict.pop(most_popular(names_dict))
- final_answer = ''.join(solution_list)
- return final_answer
- if __name__ == '__main__':
- example_names = ("Kati:F\n" * 1000 + "Mati:M\n" * 800 + "Mari:F\n" * 600 + "Tõnu:M\n" * 400).rstrip("\n").split("\n")
- people = to_dictionary(example_names)
- print(people) # -> {'Kati:F': 1000, 'Mati:M': 800, 'Mari:F': 600, 'Tõnu:M': 400}
- male_names, female_names = to_sex_dicts(people)
- print(male_names) # -> {'Mati': 800, 'Tõnu': 400}
- print(female_names) # -> {'Kati': 1000, 'Mari': 600}
- print(most_popular(male_names)) # -> "Mati"
- print(number_of_people(people)) # -> 2800
- print(names_by_popularity(male_names)) # -> 1. Mati: 800
- # 2. Tõnu: 400
- # (empty line)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement