Advertisement
Kamend1

Kamen Dimitrov - Dictionaries More Exerices - 04 Snowwhite

Nov 2nd, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. dwarves = {}
  2. hat_color_by_count = {}
  3.  
  4. while True:
  5.     user_command = input().split(" <:> ")
  6.     if user_command[0] == "Once upon a time":
  7.         break
  8.  
  9.     dwarf_name = user_command[0]
  10.     dwarf_color = user_command[1]
  11.     dwarf_physics = int(user_command[2])
  12.  
  13.     if dwarf_color not in dwarves:
  14.         dwarves[dwarf_color] = {}
  15.  
  16.     if dwarf_name not in dwarves[dwarf_color]:
  17.         dwarves[dwarf_color][dwarf_name] = dwarf_physics
  18.     else:
  19.         if dwarves[dwarf_color][dwarf_name] < dwarf_physics:
  20.             dwarves[dwarf_color][dwarf_name] = dwarf_physics
  21.  
  22. for hat_color, dwarf_list in dwarves.items():
  23.     if hat_color not in hat_color_by_count:
  24.         hat_color_by_count[hat_color] = 0
  25.     for dwarf in dwarf_list.keys():
  26.         hat_color_by_count[hat_color] += 1
  27.  
  28.  
  29. sorted_dwarves = dict(sorted(dwarves.items(), key=lambda x: max(v for v in x[1].values()), reverse=True))
  30. sorted_dwarves = dict(sorted(sorted_dwarves.items(), key=lambda x: len(x[1]), reverse=True))
  31.  
  32. for hat_color, names in sorted_dwarves.items():
  33.     for name, attributes in names.items():
  34.         print(f"({hat_color}) {name} <-> {attributes}")
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement