Advertisement
GeorgiLukanov87

01. Ranking Functions - Dictionaries - More Exercises 100/100

Jul 18th, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. # 01. Ranking - Dictionaries - More Exercises 100/100
  2. # https://judge.softuni.org/Contests/Practice/Index/1738#0
  3.  
  4.  
  5. def calculate_max_points_func():
  6.     max_points = {}
  7.     for name, points in my_dict.items():
  8.         for key, value in points.items():
  9.             if name not in max_points:
  10.                 max_points[name] = value
  11.             else:
  12.                 max_points[name] += value
  13.  
  14.     for name, score in sorted(max_points.items(), key=lambda kvp: -kvp[1]):
  15.         print(f"Best candidate is {name} with total {score} points.")
  16.         break
  17.  
  18.  
  19. def print_students_func():
  20.     print("Ranking:")
  21.     for name, score in sorted(my_dict.items(), key=lambda kvp: kvp[0]):
  22.         print(name)
  23.         for contest, points in sorted(score.items(), key=lambda kvp: -kvp[1]):
  24.             print(f"#  {contest} -> {points}")
  25.  
  26.  
  27. def adding_details_func(some_contest: str, some_name: str, some_student: int):
  28.     if some_contest in initial_dict:
  29.         if current_password in initial_dict[some_contest]:
  30.             if some_name not in my_dict:
  31.                 my_dict[some_name] = {}
  32.                 my_dict[some_name] = {some_contest: some_student}
  33.             else:
  34.                 if some_contest in my_dict[some_name].keys():
  35.                     old_points = my_dict[some_name][some_contest]
  36.                     if old_points < some_student:
  37.                         my_dict[some_name].update({some_contest: some_student})
  38.                 else:
  39.                     my_dict[some_name].update({some_contest: some_student})
  40.  
  41.  
  42. data = input()
  43. initial_dict = {}
  44.  
  45. while not data == 'end of contests':
  46.     data = data.split(":")
  47.     current_contest = data[0]
  48.     valid_password = data[1]
  49.     if current_contest not in initial_dict:
  50.         initial_dict[current_contest] = {}
  51.     initial_dict[current_contest] = valid_password
  52.     data = input()
  53.  
  54. data = input()
  55. my_dict = {}
  56.  
  57. while not data == 'end of submissions':
  58.     details = data.split("=>")
  59.     contest = details[0]
  60.     current_password = details[1]
  61.     student_name = details[2]
  62.     student_points = int(details[3])
  63.     adding_details_func(contest, student_name, student_points)
  64.     data = input()
  65.  
  66. calculate_max_points_func()
  67. print_students_func()
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement