Advertisement
differen71

Plant Discovery

Nov 25th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. """03. Plant Discovery
  2. You have now returned from your world tour. On your way, you have discovered some new plants,
  3. and you want to gather some information about them and create an exhibition to see which plant is highest rated.
  4. On the first line, you will receive a number n. On the next n lines,
  5. you will be given some information about the plants that you have discovered in the format: "{plant}<->{rarity}".
  6. Store that information because you will need it later. If you receive a plant more than once, update its rarity.
  7. After that, until you receive the command "Exhibition", you will be given some of these commands:
  8. • "Rate: {plant} - {rating}" – add the given rating to the plant (store all ratings)
  9. • "Update: {plant} - {new_rarity}" – update the rarity of the plant with the new one
  10. • "Reset: {plant}" – remove all the ratings of the given plant
  11. Note: If any given plant name is invalid, print "error"
  12. After the command "Exhibition", print the information that you have about the plants in the following format:
  13. "Plants for the exhibition:
  14. - {plant_name1}; Rarity: {rarity}; Rating: {average_rating}
  15. - {plant_name2}; Rarity: {rarity}; Rating: {average_rating}
  16. - {plant_nameN}; Rarity: {rarity}; Rating: {average_rating}"
  17. The average rating should be formatted to the second decimal place.
  18. Input / Constraints
  19. • You will receive the input as described above
  20. • JavaScript: you will receive a list of strings
  21. Output
  22. • Print the information about all plants as described above
  23. """
  24.  
  25.  
  26. def reset_rating(plants_dict, plant_name):
  27.     if plant_name in plants_dict.keys():
  28.         plants_dict[plant_name]['Rating'].clear()
  29.         plants_dict[plant_name]['Rating'].append(0)
  30.     else:
  31.         print('error')
  32.  
  33.  
  34. def rate_func(plants_dict, plant_name, plant_rating):
  35.     if plant_name not in plants_dict.keys():
  36.         print('error')
  37.     else:
  38.         plants_dict[plant_name]['Rating'].append(plant_rating)
  39.  
  40.  
  41. def add_func(plants_dict, plant_name, plant_rarity):
  42.     if plant_name not in plants_dict.keys():
  43.         plants_dict[plant_name] = {}
  44.         plants_dict[plant_name]['Rarity'] = plant_rarity
  45.         plants_dict[plant_name]['Rating'] = []
  46.     else:
  47.         plants_dict[plant_name]['Rarity'] = plant_rarity
  48.  
  49.  
  50. number_of_plants = int(input())
  51. plants = {}
  52.  
  53. for plant_count in range(number_of_plants):
  54.     plant_data = input().split('<->')
  55.     name, rarity = plant_data[0], int(plant_data[1])
  56.     if name not in plants.keys():
  57.         plants[name] = {}
  58.         plants[name]['Rarity'] = rarity
  59.         plants[name]['Rating'] = []
  60.  
  61. while True:
  62.     command_line = input().split(': ')
  63.  
  64.     if command_line[0] == 'Exhibition':
  65.         print(f"Plants for the exhibition:")
  66.         for key, value in plants.items():
  67.             average_rate = sum(value['Rating']) / len(value['Rating'])
  68.             print(f"- {key}; Rarity: {value['Rarity']}; Rating: {average_rate:.2f}")
  69.         break
  70.  
  71.     elif command_line[0] == 'Rate':
  72.         current_command = command_line[1].split(' - ')
  73.         name, rating = current_command[0], int(current_command[1])
  74.         rate_func(plants, name, rating)
  75.     elif command_line[0] == 'Update':
  76.         current_command = command_line[1].split(' - ')
  77.         name, new_rarity = current_command[0], int(current_command[1])
  78.         add_func(plants, name, new_rarity)
  79.     elif command_line[0] == 'Reset':
  80.         name = command_line[1]
  81.         reset_rating(plants, name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement