Advertisement
SimeonTs

SUPyF2 Objects/Classes-Lab - 04. Zoo

Oct 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. """
  2. Objects and Classes - Lab
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1733#3
  4.  
  5. SUPyF2 Objects/Classes-Lab - 04. Zoo
  6.  
  7. Problem:
  8. Create a class Zoo. It should have a class attribute called __animals that stores the total count of the animals
  9. in the zoo. The __init__ method should only receive the name of the zoo.
  10. There you should also create 3 empty lists (mammals, fishes, birds). The class should also have 2 more methods:
  11. • add_animal(species, name) - based on the species adds the name to the corresponding list
  12. • get_info(species) - based on the species returns a string in the following format:
  13.    "{Species} in {zoo_name}: {names}" and on another line "Total animals: {total_animals}"
  14. On the first line you will receive the name of the zoo. On the second line you will receive number n.
  15. On the next n lines you will receive animal info in the format: "{species} {name}".
  16. Add the animal to the zoo to the corresponding list. The "species" command will be mammal, fish or birds.
  17. On the final line you will receive a spices. At the end,
  18. print all the info for that species and the total count of animals.
  19.  
  20. Example:
  21. Input:
  22. Great Zoo
  23. 5
  24. mammal lion
  25. mammal bear
  26. fish salmon
  27. birds owl
  28. mammal tiger
  29. mammal
  30.  
  31. Output:
  32. Mammals in Great Zoo: lion, bear, tiger
  33. Total animals: 5
  34. """
  35.  
  36.  
  37. class Zoo:
  38.     __animals = 0
  39.  
  40.     def __init__(self, name: str):
  41.         self.name = name
  42.         self.mammals = []
  43.         self.fishes = []
  44.         self.birds = []
  45.  
  46.     def add_animal(self, species, name):
  47.         if species == "mammal":
  48.             self.mammals.append(name)
  49.         elif species == "fish":
  50.             self.fishes.append(name)
  51.         elif species == "birds":
  52.             self.birds.append(name)
  53.         self.__animals += 1
  54.  
  55.     def get_info(self, species):
  56.         result = ""
  57.         if species == "mammal":
  58.             result += f"Mammals in {self.name}: {', '.join(self.mammals)}\n"
  59.         elif species == "fish":
  60.             result += f"Fishes: {', '.join(self.fishes)}\n"
  61.         elif species == "birds":
  62.             result += f"Birds: {', '.join(self.birds)}\n"
  63.         result += f"Total animals: {zoo.__animals}"
  64.         return result
  65.  
  66.  
  67. zoo_name, count_animals = input(), int(input())
  68. zoo = Zoo(name=zoo_name)
  69. for animal in range(count_animals):
  70.     spice, animal_kind = input().split()
  71.     zoo.add_animal(species=spice, name=animal_kind)
  72. print(zoo.get_info(input()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement