Advertisement
KNenov96

Untitled

Nov 22nd, 2022 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. def get_data(from_inp):
  2.     from_inp = from_inp.split(' -> ')
  3.     name = from_inp[0]
  4.     cont = from_inp[1]
  5.     pts = int(from_inp[2])
  6.     return name, cont, pts
  7.  
  8.  
  9. def username_to_contest(record_usr, cont_parts): # takes a dictionary ordered by username and contests
  10.     record_contest = {}
  11.     for key_contest in cont_parts:
  12.         for usr in record_usr:
  13.             # print(f"{record_usr[usr][key_contest]}")
  14.             if key_contest in record_usr[usr]:
  15.                 if key_contest not in record_contest:
  16.                     record_contest[key_contest] = {usr: record_usr[usr][key_contest]}
  17.                 else:
  18.                     record_contest[key_contest][usr] = record_usr[usr][key_contest]
  19.     return record_contest
  20.  
  21.  
  22. record = {}
  23. user_inp = input()
  24. dct_contests_participants = {}
  25. # - Getting the data -
  26. while user_inp != 'no more time':
  27.     user, contest, points = get_data(user_inp)
  28.     if contest not in dct_contests_participants:  # which contest how may times
  29.         dct_contests_participants[contest] = 1
  30.     else:
  31.         dct_contests_participants[contest] += 1
  32.     if user not in record:
  33.         record[user] = {contest: points}
  34.     elif contest not in record[user]:
  35.         record[user][contest] = points
  36.     elif user in record and contest in record[user]:
  37.         dct_contests_participants[contest] -= 1
  38.         if record[user][contest] < points:
  39.             record[user][contest] = points
  40.     user_inp = input()
  41.  
  42.  
  43. dict_per_contest = username_to_contest(record, dct_contests_participants)  # changing the dictionary structure keyed by
  44.                                                                            # contest
  45.  
  46. #print(record)
  47. #print(dict_per_contest)
  48. # printing participants per contest by ascending score
  49. for exam in dct_contests_participants:
  50.     print(f"{exam}: {dct_contests_participants[exam]} participants")
  51.     i = 1
  52.     sorted_scores = dict(sorted(dict_per_contest[exam].items(), key=lambda i: (-i[1], i[0]))) # <<<<<< стар вид: key=lambda j: j[1], reverse=True
  53.     for name in sorted_scores:
  54.         print(f"{i}. {name} <::> {sorted_scores[name]}")
  55.         i += 1
  56.  
  57. # - Find personal total scores -
  58.  
  59. tot_scores = {}
  60. tot = 0
  61. for names in record:
  62.     for elem in record[names]:
  63.         tot += record[names][elem]
  64.     tot_scores[names] = tot
  65.     tot = 0
  66.  
  67. print("Individual standings:")
  68. k = 1
  69. tot_scores = dict(sorted(tot_scores.items(), key=lambda j: (-j[1], j[0]))) # <<<<<<<<< стар вид: key=lambda j: j[1], reverse=True
  70. for n in tot_scores:
  71.     print(f"{k}. {n} -> {tot_scores[n]}")
  72.     k += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement