From ff5f59474691bb9e236b556fd95bf4d5553b65a5 Mon Sep 17 00:00:00 2001 From: Andrii Silenko Date: Mon, 9 Oct 2023 15:18:36 +0300 Subject: [PATCH 1/4] Solution --- app/main.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..49f9a166 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,26 @@ -# 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 + Animal.alive.append(self) + + def __repr__(self) -> str: + return "{" + f"Name: {self.name}, " \ + f"Health: {self.health}, " \ + f"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): + herbivore.health -= 50 if not herbivore.hidden else 0 + if herbivore.health <= 0: + super().alive.remove(herbivore) From 5125c5249301a9f38ec8687efe7378f176e2ee09 Mon Sep 17 00:00:00 2001 From: Andrii Silenko Date: Wed, 11 Oct 2023 16:37:26 +0300 Subject: [PATCH 2/4] Solution with mentors advice --- app/main.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/main.py b/app/main.py index 49f9a166..bc16dbf4 100644 --- a/app/main.py +++ b/app/main.py @@ -8,9 +8,9 @@ def __init__(self, name: str, health: int = 100) -> None: Animal.alive.append(self) def __repr__(self) -> str: - return "{" + f"Name: {self.name}, " \ - f"Health: {self.health}, " \ - f"Hidden: {self.hidden}" + "}" + return ("{" + f"Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}" + "}") class Herbivore(Animal): @@ -19,8 +19,9 @@ def hide(self) -> None: class Carnivore(Animal): - def bite(self, herbivore: Herbivore) -> None: - if isinstance(herbivore, Herbivore): - herbivore.health -= 50 if not herbivore.hidden else 0 + @staticmethod + def bite(herbivore: Herbivore) -> None: + if isinstance(herbivore, Herbivore) and not herbivore.hidden: + herbivore.health -= 50 if herbivore.health <= 0: - super().alive.remove(herbivore) + super(Herbivore, herbivore).alive.remove(herbivore) From 1a0eca7f87265203f8c2cf5e2c13f0e680cc5a8a Mon Sep 17 00:00:00 2001 From: Andrii Silenko Date: Wed, 11 Oct 2023 16:51:54 +0300 Subject: [PATCH 3/4] Solution with mentors advice2 --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index bc16dbf4..9e9c4e72 100644 --- a/app/main.py +++ b/app/main.py @@ -24,4 +24,4 @@ def bite(herbivore: Herbivore) -> None: if isinstance(herbivore, Herbivore) and not herbivore.hidden: herbivore.health -= 50 if herbivore.health <= 0: - super(Herbivore, herbivore).alive.remove(herbivore) + herbivore.alive.remove(herbivore) From 139dfa12ae7ff0e67138d95c7c1ec15936681b8f Mon Sep 17 00:00:00 2001 From: Andrii Silenko Date: Thu, 19 Oct 2023 01:32:46 +0300 Subject: [PATCH 4/4] Solution --- app/main.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 9e9c4e72..bf9b5759 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,6 @@ +from __future__ import annotations + + class Animal: alive = [] @@ -8,9 +11,9 @@ def __init__(self, name: str, health: int = 100) -> None: Animal.alive.append(self) def __repr__(self) -> str: - return ("{" + f"Name: {self.name}, " - f"Health: {self.health}, " - f"Hidden: {self.hidden}" + "}") + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") class Herbivore(Animal): @@ -20,7 +23,7 @@ def hide(self) -> None: class Carnivore(Animal): @staticmethod - def bite(herbivore: Herbivore) -> None: + def bite(herbivore: Herbivore | Carnivore) -> None: if isinstance(herbivore, Herbivore) and not herbivore.hidden: herbivore.health -= 50 if herbivore.health <= 0: