SimeonTs

SUPyF2 Lists-Advanced-Exercise - 08. Feed the Animals

Oct 10th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.37 KB | None | 0 0
  1. """
  2. Lists Advanced - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1731#6
  4.  
  5. SUPyF2 Lists-Advanced-Exercise - 08. Feed the Animals (not included in final score)
  6.  
  7. Problem:
  8. The sanctuary needs to provide food for the animals and feed them, so your task is to help with the process
  9. Create a program that organizes the daily feeding of animals. You need to keep information about animals,
  10. their daily food limit and the areas of the Wildlife Refuge they live in.
  11. You will be receiving lines with commands until you receive the "Last Info" message.  There are two possible commands:
  12. • "Add:{animalName}:{dailyFoodLimit}:{area}":
  13. o   Add the animal and its daily food limit to your records.
  14.    It is guaranteed that the names of the animals are unique and there will never be animals with the same name.
  15.    If it already exists, just increase the value of the daily food limit with the current one that is given.
  16. • "Feed:{animalName}:{food}:{area}":
  17. o   Check if the animal exists and if it does, reduce its daily food limit with the given food for feeding.
  18.    If its limit reaches 0 or less, the animal is considered successfully fed and you need to remove it from
  19.    your records and print the following message:
  20.  "{animalName} was successfully fed"
  21. You need to know the count of hungry animals there are left in each area in the end.
  22. If an animal has daily food limit above 0, it is considered hungry.
  23. In the end, you have to print each animal with its daily food limit sorted in descending order by the daily food limit
  24. and then by its name in ascending order in the following format:
  25. Animals:
  26. {animalName} -> {dailyFoodLimit}g
  27. {animalName} -> {dailyFoodLimit}g
  28. Afterwards, print the areas with the count of animals, which are not fed in descending order by the count of animals.
  29. If an area has 0 hungry animals in it, don't print it. The output must be in the following format:
  30. Areas with hungry animals:
  31. {areaName} : {countOfUnfedAnimals}
  32. {areaName} : {countOfUnfedAnimals}
  33. Input / Consrtaints
  34. • You will be receiving lines until you receive the "Last Info" command.
  35. • The food comes in grams and is an integer number in the range [1...100000].
  36. • The input will always be valid.
  37. • There will never be a case, in which an animal is in two or more areas at the same time.
  38. Output
  39. • Print the appropriate message after the "Feed" command, if an animal is fed.
  40. • Print the animals with their daily food limit in the format described above.
  41. • Print the areas with the count of unfed animals in them in the format described above.
  42.  
  43. Examples:
  44. Input:
  45. Add:Maya:7600:WaterfallArea
  46. Add:Bobbie:6570:DeepWoodsArea
  47. Add:Adam:4500:ByTheCreek
  48. Add:Jamie:1290:RiverArea
  49. Add:Gem:8730:WaterfallArea
  50. Add:Maya:1230:WaterfallArea
  51. Add:Jamie:560:RiverArea
  52. Feed:Bobbie:6300:DeepWoodsArea
  53. Feed:Adam:4650:ByTheCreek
  54. Feed:Jamie:2000:RiverArea
  55. Last Info
  56.  
  57. Output:
  58. Adam was successfully fed
  59. Jamie was successfully fed
  60. Animals:
  61. Maya -> 8830g
  62. Gem -> 8730g
  63. Bobbie -> 270g
  64. Areas with hungry animals:
  65. WaterfallArea : 2
  66. DeepWoodsArea : 1
  67.  
  68. Comments:
  69. First, we receive the "Add" command, so we add "Maya" to our records and we keep her daily food limit - 7600.
  70. We know that she is in WaterfallArea. We keep adding the new animals until we receive "Maya" again and we have
  71. to increase her food limit with 1230, so it becomes 8830.
  72. After that we receive "Jamie" and we need to increase his daily food limit with 560, after which it becomes 1850.
  73. Then we start receiving "Feed" commands. First, we must decrease Bobbie's food limit with 6300,
  74. so it becomes 270. Then, we need to decrease Adam's food limit with 4650.
  75. It becomes less than zero and we remove him from the collection – he is considered fed,
  76. respectively that is one less hungry animal in the area that he is in – ByTheCreek.
  77. Then we "Feed" Jamie with 2000 and his limit becomes less than zero, so we print "Jamie was successfully fed"
  78. and we remove him from our records and note that there is one less hungry animal in his area – RiverArea. In the end,
  79. we print the animals we still have in our collection,
  80. with their daily food limits in descending order by the food limits.
  81. Afterwards we print only the areas in which there are remaining hungry animals and their count in descending order.
  82. """
  83.  
  84.  
  85. class Animal:
  86.     def __init__(self, name: str, food: int, area: str):
  87.         self.name = name
  88.         self.food = food
  89.         self.area = area
  90.  
  91.  
  92. class Area:
  93.     def __init__(self, name: str, animals: list):
  94.         self.name = name
  95.         self.animals = animals
  96.  
  97.  
  98. zoo = []
  99.  
  100. while True:
  101.     data = input()
  102.     if data == "Last Info":
  103.         break
  104.     add_or_feed, a_name, a_food, a_area = data.split(":")
  105.     if add_or_feed == "Add":
  106.         area_in_zoo = False
  107.         for area in zoo:
  108.             if area.name == a_area:
  109.                 area_in_zoo = True
  110.                 animal_in_area = False
  111.                 for animal in area.animals:
  112.                     if animal.name == a_name:
  113.                         animal_in_area = True
  114.                         animal.food += int(a_food)
  115.                         break
  116.                 if not animal_in_area:
  117.                     new_animal_for_area = Animal(name=a_name, food=int(a_food), area=a_area)
  118.                     area.animals += [new_animal_for_area]
  119.         if not area_in_zoo:
  120.             new_animal = Animal(name=a_name, food=int(a_food), area=a_area)
  121.             new_area = Area(name=a_area, animals=[new_animal])
  122.             zoo += [new_area]
  123.     elif add_or_feed == "Feed":
  124.         for area in zoo:
  125.             if area.name == a_area:
  126.                 for animal in area.animals:
  127.                     if animal.name == a_name:
  128.                         animal.food -= int(a_food)
  129.                         if animal.food <= 0:
  130.                             area.animals.remove(animal)
  131.                             print(f"{a_name} was successfully fed")
  132.  
  133. only_animals = []
  134. for area in zoo:
  135.     for animal in area.animals:
  136.         only_animals += [animal]
  137.  
  138. only_animals = sorted(sorted(only_animals, key=lambda x: x.name), key=lambda x: x.food, reverse=True)
  139. print("Animals:")
  140. for animal in only_animals:
  141.     print(f"{animal.name} -> {animal.food}g")
  142. print("Areas with hungry animals:")
  143. zoo = sorted(zoo, key=lambda x: len(x.animals), reverse=True)
  144. for area in zoo:
  145.     if len(area.animals) > 0:
  146.         print(f"{area.name} : {len(area.animals)}")
Add Comment
Please, Sign In to add comment