Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2021
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. import re
  2.  
  3. n = int(input())
  4. plants_list = {}
  5. for i in range(n):
  6.     plant, rarity = input().split("<->")
  7.     rarity = int(rarity)
  8.     if plant in plants_list:
  9.         plants_list[plant]['RARITY'] += rarity
  10.         plants_list[plant]['RATING'] = []
  11.     else:
  12.         plants_list[plant] = {'RARITY': rarity, 'RATING': []}
  13.  
  14. data = input()
  15.  
  16. while data != "Exhibition":
  17.     matches = re.split(r": | - ", data)
  18.     if matches[0] == "Rate":
  19.         plant, rating = matches[1:]
  20.         rating = int(rating)
  21.         if plant in plants_list:
  22.             plants_list[plant]['RATING'].append(rating)
  23.         else:
  24.             print("error")
  25.     elif matches[0] == "Update":
  26.         plant, new_rarity = matches[1:]
  27.         new_rarity = int(new_rarity)
  28.         if plant in plants_list:
  29.             plants_list[plant]['RARITY'] = new_rarity
  30.         else:
  31.             print("error")
  32.  
  33.     elif matches[0] == "Reset":
  34.         plant = matches[1]
  35.         if plant in plants_list:
  36.             plants_list[plant]['RATING'] = []
  37.         else:
  38.             print("error")
  39.     data = input()
  40.  
  41.  
  42. for k,v in plants_list.items():
  43.     if not plants_list[k]['RATING']:
  44.         plants_list[k]['RATING'] = 0
  45.     else:
  46.         plants_list[k]['RATING'] = sum(plants_list[k]['RATING']) / len(plants_list[k]['RATING'])
  47.  
  48.  
  49.  
  50. sorted_plants = sorted(plants_list.items(), key=lambda kvpt: (kvpt[1]["RARITY"], kvpt[1]["RATING"]), reverse=True)
  51. print("Plants for the exhibition:")
  52. for name, value in sorted_plants:
  53.     print(f"- {name}; Rarity: {value['RARITY']}; Rating: {value['RATING']:.2f}")
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement