From b196344ab7428620f2ad62a924d5211d2c347789 Mon Sep 17 00:00:00 2001 From: stefanko Date: Mon, 29 Jan 2024 12:18:11 +0200 Subject: [PATCH 1/2] solution --- app/main.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..584dde53 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,34 @@ -# write your code here +from __future__ import annotations + + +class Animal: + alive: list[Animal] = [] + + 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): + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") + + +class Herbivore(Animal): + def hide(self): + self.hidden = not self.hidden + + +class Carnivore(Animal): + @staticmethod + def bite(animal: Herbivore): + if isinstance(animal, Herbivore): + if not animal.hidden: + animal.health -= 50 + + if animal.health <= 0: + Animal.alive.remove(animal) From a00cec40ee58a3a12071b362830a44bebd3f0194 Mon Sep 17 00:00:00 2001 From: stefanko Date: Mon, 29 Jan 2024 12:23:26 +0200 Subject: [PATCH 2/2] linter fix --- app/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 584dde53..4e018bdc 100644 --- a/app/main.py +++ b/app/main.py @@ -12,20 +12,20 @@ def __init__(self, name: str, self.hidden = hidden Animal.alive.append(self) - def __repr__(self): + def __repr__(self) -> str: return (f"{{Name: {self.name}, " f"Health: {self.health}, " f"Hidden: {self.hidden}}}") class Herbivore(Animal): - def hide(self): + def hide(self) -> None: self.hidden = not self.hidden class Carnivore(Animal): @staticmethod - def bite(animal: Herbivore): + def bite(animal: Herbivore) -> None: if isinstance(animal, Herbivore): if not animal.hidden: animal.health -= 50