Nikolay9

01. Ranking

Mar 30th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. contests = {}  # contest : password, user, max(points)
  2. users = {}  # user : contest, points
  3.  
  4. while True:
  5.     command = input().split(":")
  6.     if command[0] == "end of contests":
  7.         break
  8.     contest = command[0]
  9.     password = command[1]
  10.     if contest not in contests:
  11.         contests[contest] = {"pass": password}
  12.  
  13. while True:
  14.     command = input().split("=>")
  15.     if command[0] == "end of submissions":
  16.         break
  17.     contest = command[0]
  18.     password = command[1]
  19.     user = command[2]
  20.     points = int(command[3])
  21.  
  22.     if contest not in contests or contests[contest]["pass"] != password:
  23.         continue
  24.  
  25.     if user not in contests[contest]:
  26.         contests[contest] = {"pass": password, user: points}
  27.     elif user in contests[contest] and points > contests[contest][user]:
  28.         contests[contest][user] = points
  29.  
  30.     if user not in users:
  31.         users[user] = {"sum": points, contest: points}
  32.  
  33.     elif user in users and contest not in users[user]:
  34.         users[user][contest] = points
  35.         users[user]["sum"] += points
  36.  
  37.     elif user in users and contest in users[user] and points > users[user][contest]:
  38.         users[user]["sum"] -= users[user][contest]
  39.         users[user][contest] = points
  40.         users[user]["sum"] += points
  41.  
  42. best_user = max(users, key=lambda x: users[user]["sum"])
  43. best_sum = users[best_user]["sum"]
  44.  
  45. print(f"Best candidate is {best_user} with total {best_sum} points.\n"
  46.       f"Ranking:")
  47.  
  48. for user in sorted(users):
  49.     users[user].pop("sum")
  50.     print(user)
  51.     users = {key: dict(sorted(val.items(), key=lambda ele: ele[1], reverse=True)) for key, val in users.items()}
  52.     for contest in users[user]:
  53.         print(f"#  {contest} -> {users[user][contest]}")
  54.  
Advertisement
Add Comment
Please, Sign In to add comment