From 601c16297defc2df27335a0fc7f23c1efcc31b29 Mon Sep 17 00:00:00 2001 From: Ira Date: Wed, 30 Oct 2024 09:46:02 +0200 Subject: [PATCH 1/3] Solution --- app/main.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..6b7f2351 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,32 @@ -# write your code here +class Animal: + hidden = False + alive = [] + + def __init__(self, name: str, health: int = 100) -> None: + self.name = name + self.health = health + Animal.alive.append(self) + + def __len__(self) -> int: + return len(Animal.alive) + + 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): + @classmethod + def bite(cls, goal: Animal) -> None: + if isinstance(goal, Herbivore) and not goal.hidden: + goal.health -= 50 + if goal.health <= 0: + Animal.alive.remove(goal) From 7a46b14f883b6179c2d8c35b4c0f88bbcde8c198 Mon Sep 17 00:00:00 2001 From: Ira Date: Wed, 30 Oct 2024 09:50:14 +0200 Subject: [PATCH 2/3] Solution --- app/main.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 6b7f2351..45904753 100644 --- a/app/main.py +++ b/app/main.py @@ -1,10 +1,10 @@ class Animal: - hidden = False alive = [] def __init__(self, name: str, health: int = 100) -> None: self.name = name self.health = health + self.hidden = False # Set hidden as an instance variable Animal.alive.append(self) def __len__(self) -> int: @@ -17,6 +17,23 @@ def __repr__(self) -> str: f"Hidden: {self.hidden}}}" ) + def remove_if_dead(self) -> None: + if self.health <= 0: + Animal.alive.remove(self) + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + @classmethod + def bite(cls, goal: Animal) -> None: + if isinstance(goal, Herbivore) and not goal.hidden: + goal.health -= 50 + goal.remove_if_dead() + class Herbivore(Animal): def hide(self) -> None: From 1e61d84c0fdd42c60b6394bfa64c890f56073f4d Mon Sep 17 00:00:00 2001 From: Ira Date: Wed, 30 Oct 2024 10:01:20 +0200 Subject: [PATCH 3/3] Solution --- app/main.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/app/main.py b/app/main.py index 45904753..32d074c1 100644 --- a/app/main.py +++ b/app/main.py @@ -33,17 +33,3 @@ def bite(cls, goal: Animal) -> None: if isinstance(goal, Herbivore) and not goal.hidden: goal.health -= 50 goal.remove_if_dead() - - -class Herbivore(Animal): - def hide(self) -> None: - self.hidden = not self.hidden - - -class Carnivore(Animal): - @classmethod - def bite(cls, goal: Animal) -> None: - if isinstance(goal, Herbivore) and not goal.hidden: - goal.health -= 50 - if goal.health <= 0: - Animal.alive.remove(goal)