From 6d92de91799b6c87278b6e058065e82243a92ca1 Mon Sep 17 00:00:00 2001 From: Ihor Halytskiy Date: Mon, 9 Dec 2024 20:31:26 +0200 Subject: [PATCH 1/2] Create task Herbivores and carnivores --- app/main.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..f2343029 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,55 @@ -# 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 __str__(self) -> str: + return ( + "{" + f"Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}" + "}" + ) + + def __repr__(self) -> str: + return str(self) + + @property + def health(self) -> int: + return self._health + + @health.setter + def health( + self, + new_health: int + ) -> None: + if new_health > 0: + self._health = new_health + else: + self._health = 0 + if self in Animal.alive: + Animal.alive.remove(self) + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + @staticmethod + def bite(other: Herbivore) -> None: + if isinstance(other, Herbivore): + if not other.hidden and other in Animal.alive: + other.health -= 50 From 60b5145e70d20128c37c8e5856569f7ea61263ef Mon Sep 17 00:00:00 2001 From: Ihor Halytskiy Date: Mon, 9 Dec 2024 22:07:46 +0200 Subject: [PATCH 2/2] Fix ai baddy error --- app/main.py | 52 +++++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/app/main.py b/app/main.py index f2343029..7b95b2bd 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,8 @@ +from __future__ import annotations + + class Animal: - alive = [] + alive: list[Animal] = [] def __init__( self, @@ -8,38 +11,21 @@ def __init__( hidden: bool = False ) -> None: self.name = name - self._health = health + self.health = health self.hidden = hidden + if self.health > 0: + Animal.alive.append(self) - Animal.alive.append(self) - - def __str__(self) -> str: - return ( - "{" - f"Name: {self.name}, " - f"Health: {self.health}, " - f"Hidden: {self.hidden}" - "}" - ) + def die(self) -> None: + if self.health <= 0: + if self in Animal.alive: + Animal.alive.remove(self) def __repr__(self) -> str: - return str(self) - @property - def health(self) -> int: - return self._health - - @health.setter - def health( - self, - new_health: int - ) -> None: - if new_health > 0: - self._health = new_health - else: - self._health = 0 - if self in Animal.alive: - Animal.alive.remove(self) + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") class Herbivore(Animal): @@ -48,8 +34,8 @@ def hide(self) -> None: class Carnivore(Animal): - @staticmethod - def bite(other: Herbivore) -> None: - if isinstance(other, Herbivore): - if not other.hidden and other in Animal.alive: - other.health -= 50 + def bite(self, target: Animal) -> None: + if isinstance(target, Herbivore) and not target.hidden: + target.health -= 50 + if target.health <= 0: + target.die()