From 55fcb700ada0fd326658f70086dcecf946749aff Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 9 Oct 2023 12:15:43 +0300 Subject: [PATCH 1/3] Solution --- app/main.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..f94a2d03 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,33 @@ -# 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 + self.alive.append(self) + + def __repr__(self) -> str: + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") + + def dead(self) -> None: + if self.health <= 0: + self.alive.remove(self) + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + @staticmethod + def bite(animal: Herbivore) -> None: + if isinstance(animal, Herbivore) and not animal.hidden: + animal.health -= 50 + if animal.health <= 0: + animal.dead() From 60c33e5e79497cea6020e1c5498a58d326ee7e39 Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 11 Oct 2023 12:00:04 +0300 Subject: [PATCH 2/3] class instead self --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index f94a2d03..1e585b05 100644 --- a/app/main.py +++ b/app/main.py @@ -16,7 +16,7 @@ def __repr__(self) -> str: def dead(self) -> None: if self.health <= 0: - self.alive.remove(self) + Animal.alive.remove(self) class Herbivore(Animal): From cbd4da36918278a9171e529d5e2b1351be620dac Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 11 Oct 2023 15:48:27 +0300 Subject: [PATCH 3/3] class insted self 2.0 --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 1e585b05..6fe15a3a 100644 --- a/app/main.py +++ b/app/main.py @@ -7,7 +7,7 @@ def __init__(self, name: str, self.name = name self.health = health self.hidden = hidden - self.alive.append(self) + Animal.alive.append(self) def __repr__(self) -> str: return (f"{{Name: {self.name}, "