From 95f4dcc27d48ac25d79a43a5bb7f3ef0db6ccb3d Mon Sep 17 00:00:00 2001 From: Oleksandr Pupchenko Date: Sun, 15 Oct 2023 22:26:54 +0200 Subject: [PATCH 1/2] 'Solution' --- app/main.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..9c630831 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,38 @@ -# 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 Carnivore(Animal): + + @staticmethod + def bite(herbivore: Herbivore) -> None: + if not isinstance(herbivore, Carnivore) and not herbivore.hidden: + herbivore.health -= 50 + if herbivore.health <= 0: + Animal.alive.remove(herbivore) + + +class Herbivore(Animal): + + def hide(self) -> None: + self.hidden = not self.hidden From 5cce215e255b024ec9b11b57a68e29c347dee996 Mon Sep 17 00:00:00 2001 From: Oleksandr Pupchenko Date: Wed, 18 Oct 2023 00:43:59 +0200 Subject: [PATCH 2/2] 'Solution' --- app/main.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/app/main.py b/app/main.py index 9c630831..8c3a33d1 100644 --- a/app/main.py +++ b/app/main.py @@ -1,14 +1,12 @@ -from __future__ import annotations - - class Animal: alive = [] - def __init__(self, - name: str, - health: int = 100, - hidden: bool = False) -> None: - + def __init__( + self, + name: str, + health: int = 100, + hidden: bool = False + ) -> None: self.name = name self.health = health self.hidden = hidden @@ -21,18 +19,19 @@ def __repr__(self) -> str: f"Hidden: {self.hidden}}}" ) - -class Carnivore(Animal): - - @staticmethod - def bite(herbivore: Herbivore) -> None: - if not isinstance(herbivore, Carnivore) and not herbivore.hidden: - herbivore.health -= 50 - if herbivore.health <= 0: - Animal.alive.remove(herbivore) + def die(self) -> None: + if self in Animal.alive: + Animal.alive.remove(self) class Herbivore(Animal): - def hide(self) -> None: self.hidden = not self.hidden + + +class Carnivore(Animal): + def bite(self, animal: str) -> None: + if isinstance(animal, Herbivore) and not animal.hidden: + animal.health -= 50 + if animal.health <= 0: + animal.die()