From d2f897168007cf48d6ca03edde170693c5a81375 Mon Sep 17 00:00:00 2001 From: Olexandr Hontarenko Date: Wed, 18 Oct 2023 21:05:57 +0300 Subject: [PATCH 1/2] Solution --- app/main.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..d794147d 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,29 @@ -# write your code here +class Animal: + alive = [] + + def __init__(self, name: str, health: int = 100) -> None: + self.name = name + self.health = health + self.hidden = False + if self.health > 0: + Animal.alive.append(self) + + def __repr__(self) -> repr: + name = f"Name: {self.name}, " + health = f"Health: {self.health}, " + hidden = f"Hidden: {self.hidden}" + return "{" + name + health + hidden + "}" + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = False if self.hidden else True + + +class Carnivore(Animal): + @staticmethod + def bite(animal: Herbivore) -> None: + if not animal.hidden and isinstance(animal, Herbivore): + animal.health -= 50 + if animal.health <= 0: + Animal.alive.remove(animal) From 8dfe9f87daeaa39cd1f30a4c35b78ed189d96b11 Mon Sep 17 00:00:00 2001 From: Olexandr Hontarenko Date: Thu, 19 Oct 2023 13:07:49 +0300 Subject: [PATCH 2/2] Solution --- app/main.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index d794147d..d86a8a12 100644 --- a/app/main.py +++ b/app/main.py @@ -5,19 +5,20 @@ def __init__(self, name: str, health: int = 100) -> None: self.name = name self.health = health self.hidden = False - if self.health > 0: + if self.health: Animal.alive.append(self) def __repr__(self) -> repr: - name = f"Name: {self.name}, " - health = f"Health: {self.health}, " - hidden = f"Hidden: {self.hidden}" - return "{" + name + health + hidden + "}" + return ( + f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}" + ) class Herbivore(Animal): def hide(self) -> None: - self.hidden = False if self.hidden else True + self.hidden = not self.hidden class Carnivore(Animal):