From 9fd204472f274de22f2a32ac72deddb74145a5f5 Mon Sep 17 00:00:00 2001 From: Oleksandr Akhobadze Date: Tue, 26 Nov 2024 11:30:15 +0100 Subject: [PATCH] adding solution to py-herbivores-and-carnivores --- .flake8 | 2 +- app/main.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.flake8 b/.flake8 index d7459204..e7109322 100644 --- a/.flake8 +++ b/.flake8 @@ -1,7 +1,7 @@ [flake8] inline-quotes = " ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818 -max-line-length = 79 +max-line-length = 86 max-complexity = 18 select = B,C,E,F,W,T4,B9,ANN,Q0,N8,VNE exclude = venv, tests diff --git a/app/main.py b/app/main.py index fa56336e..8ffb935e 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,31 @@ -# 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 remove_from_alive(self) -> None: + if self in Animal.alive: + Animal.alive.remove(self) + + def check(self) -> None: + if self.health <= 0: + self.remove_from_alive() + + def __repr__(self) -> str: + return f'{{Name: {self.name}, Health: {self.health}, Hidden: {self.hidden}}}' + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + def bite(self, other: "Herbivore") -> None: + if isinstance(other, Herbivore) and not other.hidden: + other.health -= 50 + other.check()