Advertisement
viligen

plant_discovery_2

Dec 3rd, 2021
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. def is_valid_plant(plants_dict, plant_name):
  2.     if plant_name in plants_dict:
  3.         return True
  4.     return print("error")
  5.  
  6.  
  7. n = int(input())
  8. plants = {}
  9. for _ in range(n):
  10.     plant, rarity = input().split("<->")
  11.     rating = []
  12.     plants[plant] = {"rarity": int(rarity), "rating": rating}
  13.  
  14. while True:
  15.     data = input().split()
  16.     if "Exhibition" in data[0]:
  17.         break
  18.     plant = data[1]
  19.     if "Rate" in data[0]:
  20.         rating = int(data[-1])
  21.         if is_valid_plant(plants, plant):
  22.             plants[plant]["rating"].append(rating)
  23.     elif "Update" in data[0]:
  24.         new_rarity = int(data[-1])
  25.         if is_valid_plant(plants, plant):
  26.             plants[plant]["rarity"] = new_rarity
  27.     elif "Reset" in data[0]:
  28.         if is_valid_plant(plants, plant):
  29.             plants[plant]["rating"] = []
  30.  
  31. for plant_, data_ in plants.items():
  32.     if data_["rating"]:
  33.         data_['rating'] = sum(data_["rating"])/len(data_['rating'])
  34.     else:
  35.         data_['rating'] = 0
  36.  
  37. sorted_plants = sorted(plants.items(), key=lambda kvp: (-kvp[1]["rarity"], -kvp[1]["rating"]))
  38. print("Plants for the exhibition:")
  39. for plant_, data_ in sorted_plants:
  40.     print(f"- {plant_}; Rarity: {data_['rarity']}; Rating: {data_['rating']:.2f}")
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement