GeorgiLukanov87

01. Ranking_dicts_more_ex

Jun 29th, 2022 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. # 01. Ranking . Dict more excercises NOT FINISHED ! 60/100 JUDGE !
  2. ********************************************************
  3.  
  4. def is_course_valid(some_dict, some_course):
  5.     if course_name in some_dict:
  6.         return True
  7.     return False
  8.  
  9.  
  10. def is_pass_valid(some_dict, current_course, current_pass):
  11.     if some_dict[current_course][0] == current_pass:
  12.         return True
  13.     return False
  14.  
  15.  
  16. def clean_pass(some_list):
  17.     for password in some_list:
  18.         password = password.pop(1)
  19.     return some_list
  20.  
  21.  
  22. def create_student_name_dict(matrix_list, students_names_dict):
  23.     student_name_dict = {}
  24.     for current_list in matrix_list:
  25.         course = current_list[0]
  26.         student_name = current_list[1]
  27.         points = int(current_list[2])
  28.         if student_name not in student_name_dict:
  29.             student_name_dict[student_name] = [[course] + [points]]
  30.         else:
  31.             if student_name_dict[student_name][0][0] == course:
  32.                 if student_name_dict[student_name][0][1] < points:
  33.                     student_name_dict[student_name][0][1] = points
  34.             else:
  35.                 student_name_dict[student_name] += [[course] + [points]]
  36.     return student_name_dict
  37.  
  38.  
  39. def max_points(some_list, name):
  40.     max_points = []
  41.     for el in some_list:
  42.         max_points.append(el[1])
  43.     max_points = sum(max_points)
  44.     return max_points, name
  45.  
  46.  
  47. data1 = input()
  48. all_students = {}
  49. all_passwords = {}
  50. all_courses = {}
  51. valid_information = []
  52. while not data1 == 'end of contests':
  53.     data1 = data1.split(':')
  54.     all_passwords[data1[0]] = [data1[1]]
  55.     all_courses[data1[0]] = {}
  56.     data1 = input()
  57. data2 = input()
  58. while not data2 == 'end of submissions':
  59.     details = data2.split('=>')
  60.     course_name = details[0]
  61.     password = details[1]
  62.     student_name = details[2]
  63.     points = int(details[3])
  64.     all_students[student_name] = {}
  65.     if (is_course_valid(all_passwords, course_name)) and (is_pass_valid(all_passwords, course_name, password)):
  66.         if student_name not in all_courses[course_name]:
  67.             valid_information.append(details)
  68.     data2 = input()
  69.  
  70. clean_pass(valid_information)
  71. result = create_student_name_dict(valid_information, all_students)
  72.  
  73. winner_name = ''
  74. winner_point = 0
  75. another_dict_try = {}
  76. for key, value in result.items():
  77.     best_points, name = max_points(value, key)
  78.     value = sorted(value, key=lambda x: x[0])
  79.     if best_points > winner_point:
  80.         winner_point = best_points
  81.         winner_name = name
  82.     another_dict_try[key] = value
  83.  
  84. print(f'Best candidate is {winner_name} with total {winner_point} points.')
  85. print('Ranking:')
  86. another_dict_try = sorted(result.items(), key=lambda y: y[0])
  87.  
  88. for name in another_dict_try:
  89.     print(name[0])
  90.     sorted_grade = sorted(name[1], key=lambda x: x[1], reverse=True)
  91.     for grade in sorted_grade:
  92.         print(f'#  {grade[0]} -> {grade[1]}')
  93.  
Add Comment
Please, Sign In to add comment