From 293ef8776c8b430c422ae397933eac6483611635 Mon Sep 17 00:00:00 2001 From: Kostia Date: Sat, 7 Oct 2023 17:26:05 +0300 Subject: [PATCH 1/5] Solution --- app/main.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..b9977685 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,40 @@ -# write your code here +class Animal: + + alive = [] + + def __init__( + self, + name: str, + health: int = 100, + hidden: bool = False + ) -> None: + self.name = name + self.health = health + self.hidden = hidden + + Animal.alive.append(self) + + def die_animal(self) -> None: + Animal.alive.remove(self) + + def __repr__(self) -> str: + return (f"{{Name: {self.name}, " + f"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, herbivore: Animal) -> None: + if isinstance(herbivore, Herbivore): + if not herbivore.hidden: + herbivore.health -= 50 + + if herbivore.health <= 0: + herbivore.die_animal() From 9413aa942cdd519e0ae377564e0dcce784fd53f1 Mon Sep 17 00:00:00 2001 From: Kostia Date: Tue, 10 Oct 2023 21:11:09 +0300 Subject: [PATCH 2/5] fixed solution --- app/main.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index b9977685..8716aff9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,4 @@ class Animal: - alive = [] def __init__( @@ -32,9 +31,9 @@ def hide(self) -> None: class Carnivore(Animal): def bite(self, herbivore: Animal) -> None: - if isinstance(herbivore, Herbivore): - if not herbivore.hidden: - herbivore.health -= 50 + if isinstance(herbivore, Herbivore) and not herbivore.hidden: + if herbivore.health <= 0: + herbivore.die_animal() + herbivore.health -= 50 + - if herbivore.health <= 0: - herbivore.die_animal() From 946c3a4c87541f6714a2aba7478e35c6713bc55b Mon Sep 17 00:00:00 2001 From: Kostia Date: Tue, 10 Oct 2023 21:12:34 +0300 Subject: [PATCH 3/5] fixed solution --- app/main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/main.py b/app/main.py index 8716aff9..c91e0472 100644 --- a/app/main.py +++ b/app/main.py @@ -35,5 +35,3 @@ def bite(self, herbivore: Animal) -> None: if herbivore.health <= 0: herbivore.die_animal() herbivore.health -= 50 - - From c6bf90beaed219d3e19509348ba90ae5c454f4df Mon Sep 17 00:00:00 2001 From: Kostia Date: Tue, 10 Oct 2023 21:18:33 +0300 Subject: [PATCH 4/5] fixed solution --- app/main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index c91e0472..e6e9c1a8 100644 --- a/app/main.py +++ b/app/main.py @@ -32,6 +32,7 @@ class Carnivore(Animal): def bite(self, herbivore: Animal) -> None: if isinstance(herbivore, Herbivore) and not herbivore.hidden: - if herbivore.health <= 0: - herbivore.die_animal() herbivore.health -= 50 + + if herbivore.health <= 0: + herbivore.die_animal() From f2bcf835776e1cbbaabd87135fb5b5015d666b5d Mon Sep 17 00:00:00 2001 From: Kostia Date: Tue, 10 Oct 2023 21:19:27 +0300 Subject: [PATCH 5/5] fixed solution --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index e6e9c1a8..330de154 100644 --- a/app/main.py +++ b/app/main.py @@ -34,5 +34,5 @@ def bite(self, herbivore: Animal) -> None: if isinstance(herbivore, Herbivore) and not herbivore.hidden: herbivore.health -= 50 - if herbivore.health <= 0: - herbivore.die_animal() + if herbivore.health <= 0: + herbivore.die_animal()