Advertisement
go6odn28

4_snow_white

Mar 11th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. def fill_dwarfs_data(dwarfs_data, dwarf_name, dwarf_hat_color, dwarf_physics):
  2.     if dwarf_hat_color not in dwarfs_data:
  3.         dwarfs_data[dwarf_hat_color] = {}
  4.     else:
  5.         if dwarf_name not in dwarfs_data[dwarf_hat_color]:
  6.             dwarfs_data[dwarf_hat_color][dwarf_name] = 0
  7.         else:
  8.             if dwarf_physics < dwarfs_data[dwarf_hat_color][dwarf_name]:
  9.                 dwarf_physics = dwarfs_data[dwarf_hat_color][dwarf_name]
  10.     dwarfs_data[dwarf_hat_color][dwarf_name] = dwarf_physics
  11.  
  12.     return dwarfs_data
  13.  
  14.  
  15. def sort_dwarfs_data(dwarfs_data):
  16.     dwarfs_dict = {}
  17.     for hat_color, members in dwarfs_data.items():
  18.         hat_length = len(members)
  19.         for dwarf, physics in members.items():
  20.             dwarf_name_color = f"{dwarf}|{hat_color}"
  21.             dwarfs_dict[dwarf_name_color] = {"name": dwarf, "physics": physics, "members": hat_length, "hat_color": hat_color}
  22.  
  23.     sorted_dwarfs_dictionary = sorted(dwarfs_dict.items(), key=lambda x: (x[1]["physics"], x[1]['members']), reverse=True)
  24.     return sorted_dwarfs_dictionary
  25.  
  26.  
  27. def print_result(sorted_dwarfs_dictionary):
  28.     result = ''
  29.     for item in sorted_dwarfs_dictionary:
  30.         current_dwarf = item[1]
  31.         result += f"({current_dwarf['hat_color']}) {current_dwarf['name']} <-> {current_dwarf['physics']}\n"
  32.  
  33.     return print(result)
  34.  
  35.  
  36. def main():
  37.     dwarfs_data = {}
  38.  
  39.     while True:
  40.         line = input()
  41.         if line == "Once upon a time":
  42.             break
  43.  
  44.         dwarf_info = line.split(" <:> ")
  45.         dwarf_name, dwarf_hat_color, dwarf_physics = dwarf_info[0], dwarf_info[1], int(dwarf_info[2])
  46.         dwarfs_data = fill_dwarfs_data(dwarfs_data, dwarf_name, dwarf_hat_color, dwarf_physics)
  47.  
  48.     sorted_dwarfs_dictionary = sort_dwarfs_data(dwarfs_data)
  49.     print_result(sorted_dwarfs_dictionary)
  50.  
  51.  
  52. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement