yordan_yordanov

Plant discovery

Dec 9th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. n = int(input())
  2.  
  3. plants = {}
  4.  
  5. for i in range(n):
  6. plant, rarity = input().split('<->')
  7.  
  8. if plant not in plants:
  9. plants[plant] = [[], []]
  10. plants[plant][0] = int(rarity)
  11.  
  12. while True:
  13. current_command = input()
  14.  
  15. if current_command == 'Exhibition':
  16. break
  17.  
  18. tokens = current_command.split(': ')
  19. command = tokens[0]
  20.  
  21. if command == 'Rate':
  22. plant, rating = tokens[1].split(' - ')
  23.  
  24. if plant in plants:
  25. plants[plant][1].append(float(rating))
  26. else:
  27. print('error')
  28.  
  29. elif command == 'Update':
  30. plant, new_rarity = tokens[1].split(' - ')
  31.  
  32. if plant in plants:
  33. plants[plant][0] = int(new_rarity)
  34. else:
  35. print('error')
  36.  
  37. elif command == 'Reset':
  38. plant = tokens[1]
  39.  
  40. if plant in plants:
  41. plants[plant][1] = []
  42. else:
  43. print('error')
  44.  
  45. else:
  46. print('error')
  47.  
  48. average_ratings = {}
  49.  
  50. for key, value in plants.items():
  51.  
  52. if len(value[1]) == 0:
  53. average = 0
  54. else:
  55. average = sum(value[1]) / len(value[1])
  56. average_ratings[key] = [value[0], average]
  57.  
  58. print('Plants for the exhibition:')
  59. sorted_plants = dict(sorted(average_ratings.items(), key=lambda x: (-x[1][0], -x[1][1])))
  60.  
  61. for key, value in sorted_plants.items():
  62. print(f'- {key}; Rarity: {value[0]}; Rating: {value[1]:.2f}')
  63.  
Advertisement
Add Comment
Please, Sign In to add comment