DiYane

Plant Discovery /old/

Sep 16th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 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. plants = {}
  14.  
  15. n = int(input())
  16. for num in range(n):
  17.     token = input().split('<->')
  18.     plant_name = token[0]
  19.     plant_rarity = token[1]
  20.     plants[plant_name] = Plant(plant_name, int(plant_rarity))
  21.  
  22. command = input()
  23. while command != 'Exhibition':
  24.     token_2 = command.split(': ')
  25.     token_3 = token_2[1].split(' - ')
  26.     plant_name = token_3[0]
  27.     command_type = token_2[0]
  28.  
  29.     if plant_name not in plants:
  30.         print('error')
  31.  
  32.     elif command_type == 'Rate':
  33.         rating = token_3[1]
  34.         plants[plant_name].ratings.append(int(rating))
  35.  
  36.     elif command_type == 'Update':
  37.         new_rarity = int(token_3[1])
  38.         plants[plant_name].rarity = new_rarity
  39.  
  40.     elif command_type == 'Reset':
  41.         plants[plant_name].ratings.clear()
  42.  
  43.     else:
  44.         print('error')
  45.  
  46.     command = input()
  47.  
  48. sorted_plants = sorted(plants.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}')
Tags: python
Add Comment
Please, Sign In to add comment