From 1eb9d66c3a3597216a85a532e1be263824cd2b52 Mon Sep 17 00:00:00 2001 From: "Alexandr K." Date: Mon, 9 Oct 2023 11:16:01 +0300 Subject: [PATCH 1/3] Solution --- app/main.py | 31 ++++++++++++++++++++++++++++++- tests/test_main.py | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index fa56336e..d4a6b83f 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,30 @@ -# write your code here +class Animal: + alive = [] + + def __init__(self, name: str, health: int = 100, + hidden: bool = False) -> None: + self.health = health + self.name = name + self.hidden = hidden + Animal.alive.append(self) + + def __repr__(self) -> str: + return (f"{{Name: {self.name}, Health: {self.health}, " + f"Hidden: {self.hidden}}}") + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + def bite(self, animal: Animal) -> None: + if (animal.hidden or animal not in animal.alive + or not isinstance(animal, Herbivore)): + return + animal.health -= 50 + if animal.health <= 0: + for item in Animal.alive: + if item.name == animal.name: + Animal.alive.remove(item) diff --git a/tests/test_main.py b/tests/test_main.py index dcdf42cd..78269225 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -15,7 +15,7 @@ def test_animal_class(): def test_animal_constructor(): - lion = Animal("Lion King") + lion = Animal("Lion King", ) assert hasattr(lion, "name"), ( "Animal instance should have attribute 'name'" ) From aa29129538fac5a08214540d4313ff7c4c16c961 Mon Sep 17 00:00:00 2001 From: "Alexandr K." Date: Mon, 9 Oct 2023 13:10:52 +0300 Subject: [PATCH 2/3] BugFix1 --- app/main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index d4a6b83f..2ce159e9 100644 --- a/app/main.py +++ b/app/main.py @@ -25,6 +25,4 @@ def bite(self, animal: Animal) -> None: return animal.health -= 50 if animal.health <= 0: - for item in Animal.alive: - if item.name == animal.name: - Animal.alive.remove(item) + Animal.alive.remove(animal) From 75f7549245eab5edac4aa0d944c0bc15e7ac4845 Mon Sep 17 00:00:00 2001 From: "Alexandr K." Date: Mon, 9 Oct 2023 13:46:19 +0300 Subject: [PATCH 3/3] BugFix2 --- app/main.py | 11 +++++------ tests/test_main.py | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/main.py b/app/main.py index 2ce159e9..9072e2c6 100644 --- a/app/main.py +++ b/app/main.py @@ -20,9 +20,8 @@ def hide(self) -> None: class Carnivore(Animal): def bite(self, animal: Animal) -> None: - if (animal.hidden or animal not in animal.alive - or not isinstance(animal, Herbivore)): - return - animal.health -= 50 - if animal.health <= 0: - Animal.alive.remove(animal) + if (not animal.hidden and animal in animal.alive + and isinstance(animal, Herbivore)): + animal.health -= 50 + if animal.health <= 0: + Animal.alive.remove(animal) diff --git a/tests/test_main.py b/tests/test_main.py index 78269225..dcdf42cd 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -15,7 +15,7 @@ def test_animal_class(): def test_animal_constructor(): - lion = Animal("Lion King", ) + lion = Animal("Lion King") assert hasattr(lion, "name"), ( "Animal instance should have attribute 'name'" )