Arksiana

01.Ranking / Dictionaries - More Exercises 100/100

Nov 17th, 2020 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1.  
  2. contests = {}
  3.  
  4. data = input()
  5. while not data == 'end of contests':
  6.     contest, password = data.split(":")
  7.  
  8.     if contest not in contests:
  9.         contests[contest] = password
  10.     data = input()
  11.  
  12. username_dict = {}
  13.  
  14. data = input()
  15. while not data == 'end of submissions':
  16.     contest, password, username, points = data.split("=>")
  17.     points = int(points)
  18.     is_valid = False
  19.     if contest in contests:
  20.         if contests[contest] == password:
  21.             is_valid = True
  22.  
  23.     if is_valid:
  24.  
  25.         if username not in username_dict:
  26.             username_dict[username] = {}
  27.             username_dict[username][contest] = points
  28.         else:
  29.             if contest in username_dict[username]:
  30.                 if username_dict[username][contest] < points:
  31.                     username_dict[username][contest] = points
  32.             else:
  33.                 username_dict[username][contest] = points
  34.  
  35.     data = input()
  36.  
  37. max_points = 0
  38. max_name = ''
  39. for name, value in username_dict.items():
  40.     sum_points = 0
  41.     for key in value:
  42.         sum_points += value[key]
  43.     if sum_points > max_points:
  44.         max_points = sum_points
  45.         max_name = name
  46.  
  47. print(f"Best candidate is {max_name} with total {max_points} points.")
  48.  
  49. sorted_dict = dict(sorted(username_dict.items(), key=lambda x: x[0]))
  50.  
  51. # SORT NESTED DICT BY VALUE
  52. print("Ranking:")
  53. for name, value in sorted_dict.items():
  54.     print(f"{name}")
  55.  
  56.     sorted_items = dict(sorted(value.items(), key=lambda x: (-x[1], x[0])))
  57.     for k2, v2 in sorted_items.items():
  58.         print(f'#  {k2} -> {v2}')
  59.  
  60.  
Advertisement
Add Comment
Please, Sign In to add comment