From c5fbabbba7befeb057270b79a35b5f6dc0a671c7 Mon Sep 17 00:00:00 2001 From: Yulia Date: Tue, 8 Oct 2024 07:09:49 +0300 Subject: [PATCH 1/2] Solution --- app/main.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 8fbf3053..139447a3 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,65 @@ -# write your code here +class Animal: + def __init__(self, name: str, appetite: int, + is_hungry: bool = True) -> None: + self.name = name + self.appetite = appetite + self.is_hungry = is_hungry + + def print_name(self) -> None: + print(f"Hello, I'm {self.name}") + + def feed(self) -> int: + if self.is_hungry: + print(f"Eating {self.appetite} food points...") + self.is_hungry = False + return self.appetite + return 0 + + +class Cat(Animal): + def __init__(self, name: str, is_hungry: bool = True) -> None: + super().__init__(name, 3, is_hungry) + + def catch_mouse(self) -> None: + print("The hunt began!") + + +class Dog(Animal): + def __init__(self, name: str, is_hungry: bool = True) -> None: + super().__init__(name, 7, is_hungry) + + def bring_slippers(self) -> None: + print("The slippers delivered!") + + +def feed_animals(animals: list[Animal]) -> int: + total_food = 0 + for animal in animals: + total_food += animal.feed() + return total_food + + +lion = Animal("Lion", 25) +lion.print_name() +food_points = lion.feed() +print(food_points) +print(lion.is_hungry) +print(lion.feed()) + +cat = Cat("Cat") +cat.print_name() +cat.feed() + +cat2 = Cat("Cat", False) +print(cat2.feed()) +cat2.catch_mouse() + +dog = Dog("Dog") +dog.print_name() +dog.feed() + +dog2 = Dog("Dog", False) +print(dog2.feed()) +dog2.bring_slippers() + +print(feed_animals([cat2, lion, dog])) From 58012b6fe3de61a18c88b82017b6b670c2586cce Mon Sep 17 00:00:00 2001 From: Yulia Date: Wed, 9 Oct 2024 17:13:45 +0300 Subject: [PATCH 2/2] Solution --- app/main.py | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/app/main.py b/app/main.py index 139447a3..2c16cfb9 100644 --- a/app/main.py +++ b/app/main.py @@ -33,33 +33,4 @@ def bring_slippers(self) -> None: def feed_animals(animals: list[Animal]) -> int: - total_food = 0 - for animal in animals: - total_food += animal.feed() - return total_food - - -lion = Animal("Lion", 25) -lion.print_name() -food_points = lion.feed() -print(food_points) -print(lion.is_hungry) -print(lion.feed()) - -cat = Cat("Cat") -cat.print_name() -cat.feed() - -cat2 = Cat("Cat", False) -print(cat2.feed()) -cat2.catch_mouse() - -dog = Dog("Dog") -dog.print_name() -dog.feed() - -dog2 = Dog("Dog", False) -print(dog2.feed()) -dog2.bring_slippers() - -print(feed_animals([cat2, lion, dog])) + return sum(animal.feed() for animal in animals)