bl00dt3ars

Untitled

Aug 16th, 2021 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. places = {}
  2. animals = {}
  3. while True:
  4.     command = input()
  5.     if command == "EndDay":
  6.         break
  7.     data = command.split(": ")
  8.     action = data[0]
  9.     if action == "Add":
  10.         name, food, area = data[1].split('-')
  11.         food = int(food)
  12.         if name not in animals:
  13.             animals[name] = {'food': food, 'area': area}
  14.         else:
  15.             animals[name]['food'] += food
  16.         if area not in places:
  17.             places[area] = []
  18.         if name not in places[area]:
  19.             places[area].append(name)
  20.     elif action == "Feed":
  21.         name, food = data[1].split('-')
  22.         food = int(food)
  23.         if name in animals:
  24.             animals[name]['food'] -= food
  25.             if animals[name]['food'] <= 0:
  26.                 animals.pop(name)
  27.                 print(f"{name} was successfully fed!")
  28.                 for area in places:
  29.                     if name in places[area]:
  30.                         places[area] = [el for el in places[area] if not el == name]
  31.                 places = {area: name for area, name in places.items() if not len(name) == 0}
  32.  
  33. # for name, area in animals.items():
  34. #     new = area['area']
  35. #     if new not in places:
  36. #         places[new] = [name]
  37. #     else:
  38. #         places[new].append(name)
  39.  
  40. animals = sorted(animals.items(), key=lambda x: (-x[1]['food'], x[0]))
  41. places = sorted(places.items(), key=lambda x: (-len(x[1]), x[0]))
  42. print(f"Animals:")
  43. for name, food in animals:
  44.     print(f" {name} -> {food['food']}g")
  45. print("Areas with hungry animals:")
  46. for area, animals in places:
  47.     print(f" {area}: {len(animals)}")
Add Comment
Please, Sign In to add comment