From 12936ec404b8c267f250657f96212c8cf0212d8c Mon Sep 17 00:00:00 2001 From: Kitaiskyi Oleksii Date: Wed, 27 Sep 2023 14:41:45 +0300 Subject: [PATCH 1/2] solution --- app/main.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..fa330947 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,38 @@ -# 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 __str__(self) -> str: + + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") + + def __repr__(self) -> str: + return self.__str__() + + +class Herbivore(Animal): + + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + + @classmethod + def bite(cls, victim: "Herbivore") -> None: + if not isinstance(victim, Carnivore) and not victim.hidden: + victim.health -= 50 + if victim.health <= 0: + cls.alive.remove(victim) From 7ababe92e261aa4443dc0f81d606a49554775239 Mon Sep 17 00:00:00 2001 From: Kitaiskyi Oleksii Date: Tue, 10 Oct 2023 17:01:34 +0300 Subject: [PATCH 2/2] repr --- app/main.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index fa330947..e41c34ab 100644 --- a/app/main.py +++ b/app/main.py @@ -10,17 +10,14 @@ def __init__(self, name: str, self.health = health self.hidden = hidden - self.alive.append(self) + Animal.alive.append(self) - def __str__(self) -> str: + def __repr__(self) -> str: return (f"{{Name: {self.name}, " f"Health: {self.health}, " f"Hidden: {self.hidden}}}") - def __repr__(self) -> str: - return self.__str__() - class Herbivore(Animal): @@ -31,7 +28,7 @@ def hide(self) -> None: class Carnivore(Animal): @classmethod - def bite(cls, victim: "Herbivore") -> None: + def bite(cls, victim: Herbivore) -> None: if not isinstance(victim, Carnivore) and not victim.hidden: victim.health -= 50 if victim.health <= 0: