From 66902c60486c56351dd04340b1a1754d0d7e7042 Mon Sep 17 00:00:00 2001 From: mykolamateichuk Date: Mon, 9 Oct 2023 02:02:35 +0300 Subject: [PATCH 1/3] Solution --- app/main.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..aa484309 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,25 @@ -# 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 __repr__(self) -> str: + return f"{{Name: {self.name}, Health: {self.health}, Hidden: {self.hidden}}}" + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + def bite(self, animal: Animal) -> None: + if isinstance(animal, Herbivore) and not animal.hidden: + animal.health -= 50 + if animal.health <= 0: + Animal.alive.remove(animal) From ae4cf1a5af165a44c26c7227e1bf727fb022dda5 Mon Sep 17 00:00:00 2001 From: mykolamateichuk Date: Mon, 9 Oct 2023 02:05:09 +0300 Subject: [PATCH 2/3] Solution --- app/main.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index aa484309..25c7f06a 100644 --- a/app/main.py +++ b/app/main.py @@ -1,7 +1,10 @@ class Animal: alive = [] - def __init__(self, name: str, health: int = 100, hidden: bool = False) -> None: + def __init__(self, + name: str, + health: int = 100, + hidden: bool = False) -> None: self.name = name self.health = health self.hidden = hidden @@ -9,7 +12,9 @@ def __init__(self, name: str, health: int = 100, hidden: bool = False) -> None: Animal.alive.append(self) def __repr__(self) -> str: - return f"{{Name: {self.name}, Health: {self.health}, Hidden: {self.hidden}}}" + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") class Herbivore(Animal): From c113be5c36b9f36c9be8f8dd491a5fd47fc28e39 Mon Sep 17 00:00:00 2001 From: mykolamateichuk Date: Mon, 9 Oct 2023 02:09:17 +0300 Subject: [PATCH 3/3] Solution --- app/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 25c7f06a..d61fc68f 100644 --- a/app/main.py +++ b/app/main.py @@ -23,7 +23,8 @@ def hide(self) -> None: class Carnivore(Animal): - def bite(self, animal: Animal) -> None: + @staticmethod + def bite(animal: Animal) -> None: if isinstance(animal, Herbivore) and not animal.hidden: animal.health -= 50 if animal.health <= 0: