Advertisement
simeonshopov

Feeding the Animals

Jan 14th, 2020
2,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. command = input().split(':')
  2. animals = {}
  3. areas = {}
  4.  
  5.  
  6. while True:
  7.   if command[0] == 'Last Info':
  8.     break
  9.   command_type = command[0]
  10.   animal = command[1]
  11.   food = int(command[2])
  12.   area = command[3]
  13.   if command_type == 'Add':
  14.     if area in areas:
  15.       if animal not in animals:
  16.         areas[area] += 1
  17.     else:
  18.       areas[area] = 1
  19.     if animal in animals:
  20.       animals[animal] += food
  21.     else:
  22.       animals[animal] = food
  23.   elif command_type == 'Feed':
  24.     if animal in animals:
  25.       animals[animal] -= food
  26.       if animals[animal] <= 0:
  27.         print(f'{animal} was successfully fed')
  28.         del animals[animal]
  29.         areas[area] -= 1
  30.       if areas[area] <= 0:
  31.         del areas[area]
  32.      
  33.   command = input().split(':')
  34.  
  35. animals_sorted = sorted(animals, key=animals.get, reverse=True)
  36. areas_sorted = sorted(areas, key=areas.get, reverse=True)
  37. print('Animals:')
  38. for i in animals_sorted:
  39.   print(f'{i} -> {animals[i]}g')
  40. print('Areas with hungry animals:')
  41. for i in areas_sorted:
  42.   print(f'{i} : {areas[i]}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement