From e8508891058a9ffabb06e5eed5813a120492a371 Mon Sep 17 00:00:00 2001 From: Ruslan Misko Date: Mon, 9 Oct 2023 14:36:13 +0300 Subject: [PATCH 1/4] Solution --- app/main.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..2642ab8e 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,30 @@ -# 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) + + @staticmethod + def check_alive_animals() -> None: + Animal.alive = [animal for animal in Animal.alive if animal.health > 0] + + 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, herbivore: Herbivore) -> None: + if isinstance(herbivore, Herbivore) is False: + return + if herbivore.hidden is True or herbivore.health < 1: + return + herbivore.health -= 50 + self.check_alive_animals() From 9bcc8073b5669abb79857d109c72f961b587ca5f Mon Sep 17 00:00:00 2001 From: Ruslan Misko Date: Mon, 9 Oct 2023 14:39:19 +0300 Subject: [PATCH 2/4] Solution --- app/main.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 2642ab8e..b326463e 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 @@ -12,7 +15,9 @@ def check_alive_animals() -> None: Animal.alive = [animal for animal in Animal.alive if animal.health > 0] 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 72a62406543816f91c6d355374ed0a346b17a310 Mon Sep 17 00:00:00 2001 From: Ruslan Misko Date: Mon, 9 Oct 2023 14:41:16 +0300 Subject: [PATCH 3/4] last Solution --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index b326463e..2776a325 100644 --- a/app/main.py +++ b/app/main.py @@ -15,8 +15,8 @@ def check_alive_animals() -> None: Animal.alive = [animal for animal in Animal.alive if animal.health > 0] def __repr__(self) -> str: - return (f"{{Name: {self.name}," - f"Health: {self.health}," + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " f"Hidden: {self.hidden}}}") From ed32feba99abe23a4d2e1b408ddbf60535fad988 Mon Sep 17 00:00:00 2001 From: Ruslan Misko Date: Tue, 10 Oct 2023 22:42:12 +0300 Subject: [PATCH 4/4] Solution fix --- app/main.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/main.py b/app/main.py index 2776a325..0fbfbd56 100644 --- a/app/main.py +++ b/app/main.py @@ -26,10 +26,8 @@ def hide(self) -> None: class Carnivore(Animal): - def bite(self, herbivore: Herbivore) -> None: - if isinstance(herbivore, Herbivore) is False: - return - if herbivore.hidden is True or herbivore.health < 1: - return - herbivore.health -= 50 - self.check_alive_animals() + @staticmethod + def bite(herbivore: Herbivore) -> None: + if isinstance(herbivore, Herbivore) and herbivore.hidden is False: + herbivore.health -= 50 + Animal.check_alive_animals()