Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """03. Plant Discovery
- You have now returned from your world tour. On your way, you have discovered some new plants,
- and you want to gather some information about them and create an exhibition to see which plant is highest rated.
- On the first line, you will receive a number n. On the next n lines,
- you will be given some information about the plants that you have discovered in the format: "{plant}<->{rarity}".
- Store that information because you will need it later. If you receive a plant more than once, update its rarity.
- After that, until you receive the command "Exhibition", you will be given some of these commands:
- • "Rate: {plant} - {rating}" – add the given rating to the plant (store all ratings)
- • "Update: {plant} - {new_rarity}" – update the rarity of the plant with the new one
- • "Reset: {plant}" – remove all the ratings of the given plant
- Note: If any given plant name is invalid, print "error"
- After the command "Exhibition", print the information that you have about the plants in the following format:
- "Plants for the exhibition:
- - {plant_name1}; Rarity: {rarity}; Rating: {average_rating}
- - {plant_name2}; Rarity: {rarity}; Rating: {average_rating}
- …
- - {plant_nameN}; Rarity: {rarity}; Rating: {average_rating}"
- The average rating should be formatted to the second decimal place.
- Input / Constraints
- • You will receive the input as described above
- • JavaScript: you will receive a list of strings
- Output
- • Print the information about all plants as described above
- """
- def reset_rating(plants_dict, plant_name):
- if plant_name in plants_dict.keys():
- plants_dict[plant_name]['Rating'].clear()
- plants_dict[plant_name]['Rating'].append(0)
- def rate_func(plants_dict, plant_name, plant_rating):
- if plant_name not in plants_dict.keys():
- print('error')
- else:
- plants_dict[plant_name]['Rating'].append(plant_rating)
- def add_func(plants_dict, plant_name, plant_rarity):
- if plant_name not in plants_dict.keys():
- plants_dict[plant_name] = {}
- plants_dict[plant_name]['Rarity'] = plant_rarity
- plants_dict[plant_name]['Rating'] = []
- else:
- plants_dict[plant_name]['Rarity'] = plant_rarity
- number_of_plants = int(input())
- plants = {}
- for plant_count in range(number_of_plants):
- plant_data = input().split('<->')
- name = plant_data[0]
- rarity = int(plant_data[1])
- if name not in plants.keys():
- plants[name] = {}
- plants[name]['Rarity'] = rarity
- plants[name]['Rating'] = []
- while True:
- command_line = input().split(': ')
- if command_line[0] == 'Exhibition':
- print(f"Plants for the exhibition:")
- for key, value in plants.items():
- average_rate = sum(value['Rating']) / len(value['Rating'])
- print(f"- {key}; Rarity: {value['Rarity']}; Rating: {average_rate:.2f}")
- break
- elif command_line[0] == 'Rate':
- name, rating = command_line[1].split(' - ')
- rate_func(plants, name, int(rating))
- elif command_line[0] == 'Update':
- name, new_rarity = command_line[1].split(' - ')
- add_func(plants, name, new_rarity)
- elif command_line[0] == 'Reset':
- name = command_line[1]
- reset_rating(plants, name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement