From 6fc021445cabffd4bad0a355d75a07b268fab149 Mon Sep 17 00:00:00 2001 From: Roman Peka Date: Sun, 22 Dec 2024 23:00:35 +0300 Subject: [PATCH 1/2] Solution --- app/main.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..7b474d54 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,35 @@ -# write your code here +from __future__ import annotations + + +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 __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 = False if self.hidden else True + + +class Carnivore(Animal): + @staticmethod + def bite(other: Animal) -> None: + if isinstance(other, Herbivore) and not other.hidden: + other.health -= 50 + if other.health <= 0: + Animal.alive.remove(other) From d5fa196a3c9cb1028edff200053999fc190d6b1f Mon Sep 17 00:00:00 2001 From: Roman Peka Date: Sun, 22 Dec 2024 23:12:05 +0300 Subject: [PATCH 2/2] Solution --- app/main.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 7b474d54..906713a3 100644 --- a/app/main.py +++ b/app/main.py @@ -23,13 +23,12 @@ def __repr__(self) -> str: class Herbivore(Animal): def hide(self) -> None: - self.hidden = False if self.hidden else True + self.hidden = not self.hidden class Carnivore(Animal): - @staticmethod - def bite(other: Animal) -> None: + def bite(self, other: Animal) -> None: if isinstance(other, Herbivore) and not other.hidden: other.health -= 50 - if other.health <= 0: + if other.health <= 0 and other in Animal.alive: Animal.alive.remove(other)