Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2021
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. # 02. Programming Fundamentals Final Exam -  3. Plant Discovery:
  2. num = int(input())
  3. plants = {}
  4. for i in range(num):
  5.     command = input().split("<->")
  6.     plant = command[0]
  7.     rarity = int(command[1])
  8.     if plant not in plants.keys():
  9.         plants[plant] = []
  10.         plants[plant].append(rarity)
  11.     else:
  12.         plants[plant] = []
  13.         plants[plant].append(rarity)
  14.  
  15. while True:
  16.     command = input()
  17.     if command == "Exhibition":
  18.         break
  19.     current_command = command.split(": ")[0]
  20.     second_part = command.split(": ")[1]
  21.     if "Rate" in command:
  22.         plant_to_be_rated = second_part.split(" - ")[0]
  23.         rating_plant = int(second_part.split(" - ")[1])
  24.         if plant_to_be_rated not in plants.keys():
  25.             print("error")
  26.         else:
  27.             plants[plant_to_be_rated].append(rating_plant)
  28.     elif "Update" in command:
  29.         plant_tobe_updated = second_part.split(" - ")[0]
  30.         if plant_tobe_updated not in plants.keys():
  31.             print("error")
  32.         else:
  33.             new_rarity = int(second_part.split(" - ")[1])
  34.             plants[plant_tobe_updated].pop(0)
  35.             plants[plant_tobe_updated].insert(0, new_rarity)
  36.     elif "Reset" in command:
  37.         plant_to_be_reset = command.split(": ")[1]
  38.         if plant_to_be_reset not in plants.keys():
  39.             print("error")
  40.         else:
  41.             rarity_to_add_back = plants[plant_to_be_reset][0]
  42.             plants[plant_to_be_reset].clear()
  43.             plants[plant_to_be_reset].append(rarity_to_add_back)
  44.             #plants[plant_to_be_reset].insert(1, 0)
  45.     else:
  46.         print("error")
  47.  
  48. for key, value in plants.items():
  49.     item_to_amend = value[1:]
  50.     remaining_part = value[0]
  51.     if len(item_to_amend) > 1:
  52.         total = sum(item_to_amend)
  53.         new_num = total / len(item_to_amend)
  54.         value.clear()
  55.         value.append(remaining_part)
  56.         value.append(new_num)
  57.     if len(value) == 1:
  58.         value.append(0)
  59.  
  60. print(f"Plants for the exhibition:")
  61. plants = dict(sorted(plants.items(), key=lambda x: (-x[1][0], -x[1][1])))
  62. for key, value in plants.items():
  63.     print(f"- {key}; Rarity: {int(value[0])}; Rating: {value[1]:.2f}")
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement