From 907c452eea3714a537b32621fb3d571e2b1b3cd7 Mon Sep 17 00:00:00 2001 From: sakhaline Date: Sun, 8 Oct 2023 15:15:16 +0300 Subject: [PATCH 1/2] Solution --- app/main.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..3af4595e 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,35 @@ -# 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 + __class__.alive.append(self) + + def __repr__(self) -> str: + return ( + f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}" + ) + + def remove_dead_beast(self) -> None: + if self.health <= 0: + __class__.alive.remove(self) + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + def bite(self, victim: "Herbivore") -> None: + if isinstance(victim, Herbivore) and not victim.hidden: + victim.health -= 50 + victim.remove_dead_beast() From 0223a6e7b0b212b9d51fcd62903de7b2b542822d Mon Sep 17 00:00:00 2001 From: sakhaline Date: Tue, 10 Oct 2023 20:19:28 +0300 Subject: [PATCH 2/2] Add staticmethod --- app/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 3af4595e..e1f63999 100644 --- a/app/main.py +++ b/app/main.py @@ -29,7 +29,8 @@ def hide(self) -> None: class Carnivore(Animal): - def bite(self, victim: "Herbivore") -> None: + @staticmethod + def bite(victim: Herbivore) -> None: if isinstance(victim, Herbivore) and not victim.hidden: victim.health -= 50 victim.remove_dead_beast()