From cc5b75b93d18752e8fa26eb594aa7c057ac1d8fb Mon Sep 17 00:00:00 2001 From: V_core Date: Sun, 24 Sep 2023 16:18:33 +0300 Subject: [PATCH 1/5] solution --- app/main.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..fd829d3b 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,38 @@ -# write your code here +from typing import Union + + +class Animal: + alive = [] + + def __init__( + self, + name: str, + health: int = 100, + hidden: bool = False) -> None: + self.name = name + self.health = health + self.hidden = hidden + self.alive.append(self) + + @classmethod + def remove_dead_animal(cls) -> None: + cls.alive = [animal for animal in cls.alive if animal.health > 0] + + def __repr__(self) -> str: + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") + + +class Carnivore(Animal): + + def bite(self, target: Union) -> None: + target.health -= 50 \ + if (isinstance(target, Herbivore) and not target.hidden) else 0 + Animal.remove_dead_animal() + + +class Herbivore(Animal): + + def hide(self) -> None: + self.hidden = not self.hidden From a71a2df051c11e1b46f58ec53825529eb440d39d Mon Sep 17 00:00:00 2001 From: V_core Date: Tue, 17 Oct 2023 23:07:04 +0300 Subject: [PATCH 2/5] fixed --- app/main.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/app/main.py b/app/main.py index fd829d3b..eea9315f 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,5 @@ -from typing import Union +from __future__ import annotations +from typing import Type class Animal: @@ -8,15 +9,16 @@ def __init__( self, name: str, health: int = 100, - hidden: bool = False) -> None: + hidden: bool = False + ) -> None: self.name = name self.health = health self.hidden = hidden self.alive.append(self) @classmethod - def remove_dead_animal(cls) -> None: - cls.alive = [animal for animal in cls.alive if animal.health > 0] + def remove_dead_animal(cls, animal: Type[Herbivore]) -> None: + cls.alive.remove(animal) def __repr__(self) -> str: return (f"{{Name: {self.name}, " @@ -24,15 +26,16 @@ def __repr__(self) -> str: f"Hidden: {self.hidden}}}") -class Carnivore(Animal): - - def bite(self, target: Union) -> None: - target.health -= 50 \ - if (isinstance(target, Herbivore) and not target.hidden) else 0 - Animal.remove_dead_animal() - - class Herbivore(Animal): def hide(self) -> None: self.hidden = not self.hidden + + +class Carnivore(Animal): + + def bite(self, target: Type[Herbivore]) -> None: + if (isinstance(target, Herbivore)) and (not target.hidden): + target.health -= 50 + if target.health <= 0: + Animal.remove_dead_animal(target) From 7e963acf22b5f14d4a7185af2f75dfafb2338dea Mon Sep 17 00:00:00 2001 From: V_core Date: Sun, 22 Oct 2023 21:31:56 +0300 Subject: [PATCH 3/5] fixed --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index eea9315f..583ab2d3 100644 --- a/app/main.py +++ b/app/main.py @@ -14,7 +14,7 @@ def __init__( self.name = name self.health = health self.hidden = hidden - self.alive.append(self) + Animal.alive.append(self) @classmethod def remove_dead_animal(cls, animal: Type[Herbivore]) -> None: From 31445bd44b69d6cb4d26b4c7bf93a2c6d412e875 Mon Sep 17 00:00:00 2001 From: V_core Date: Fri, 17 Nov 2023 22:59:23 +0200 Subject: [PATCH 4/5] fixes --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 583ab2d3..8a7eeca9 100644 --- a/app/main.py +++ b/app/main.py @@ -17,7 +17,7 @@ def __init__( Animal.alive.append(self) @classmethod - def remove_dead_animal(cls, animal: Type[Herbivore]) -> None: + def remove_dead_animal(cls, animal: Herbivore) -> None: cls.alive.remove(animal) def __repr__(self) -> str: @@ -34,7 +34,7 @@ def hide(self) -> None: class Carnivore(Animal): - def bite(self, target: Type[Herbivore]) -> None: + def bite(self, target: Herbivore) -> None: if (isinstance(target, Herbivore)) and (not target.hidden): target.health -= 50 if target.health <= 0: From c350c23cd348ae95a711c36ef31aa7a1888a3b6f Mon Sep 17 00:00:00 2001 From: V_core Date: Fri, 17 Nov 2023 23:00:37 +0200 Subject: [PATCH 5/5] fixes --- app/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/main.py b/app/main.py index 8a7eeca9..f34395b8 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,4 @@ from __future__ import annotations -from typing import Type class Animal: