Advertisement
BdW44222

03. Plant Discovery

Aug 10th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. class Plant:
  2. def __init__(self, name, rarity):
  3. self.name = name
  4. self.rarity = rarity
  5. self.ratings = []
  6.  
  7. def rating(self):
  8. if self.ratings:
  9. return sum(self.ratings) / len(self.ratings)
  10. return 0
  11.  
  12.  
  13. n = int(input())
  14.  
  15. plants_book = {}
  16.  
  17. for i in range(n):
  18. plants_info = input()
  19. plant_name = plants_info.split("<->")[0]
  20. plant_rarity = int(plants_info.split("<->")[1])
  21. plants_book[plant_name] = Plant(plant_name, int(plant_rarity))
  22.  
  23.  
  24. commands = input()
  25. ratings_count = 0
  26.  
  27. while not commands == "Exhibition":
  28. command = commands.split(": ")[0]
  29. second_part = commands.split(": ")[1]
  30. plant_name = second_part.split(" - ")[0]
  31.  
  32. if plant_name not in plants_book:
  33. print('error')
  34.  
  35. elif command == "Rate":
  36. rating = int(second_part.split(" - ")[1])
  37. plants_book[plant_name].ratings.append(int(rating))
  38.  
  39. elif command == "Update":
  40. new_rarity = int(second_part.split(" - ")[1])
  41. plants_book[plant_name].rarity = new_rarity
  42.  
  43. elif command == "Reset":
  44. plants_book[plant_name].ratings.clear()
  45.  
  46. commands = input()
  47.  
  48. sorted_plants = sorted(plants_book.values(), key=lambda p: (p.rarity, p.rating()), reverse=True)
  49.  
  50. print(f'Plants for the exhibition:')
  51. for plant in sorted_plants:
  52. print(f'- {plant.name}; Rarity: {plant.rarity}; Rating: {plant.rating():.2f}')
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement