From 8be91e05ebcdcf4f413f2e8e378723f2f94e2b5e Mon Sep 17 00:00:00 2001 From: Denys Kibalenko Date: Tue, 3 Oct 2023 12:47:27 +0200 Subject: [PATCH 1/2] Solution --- app/main.py | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..818c54d5 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,101 @@ -# write your code here +class Animal: + """ + Represents a basic Animal creature, stores all its instances. + + Attributes: + ---------- + name : str + the name of the animal + health : int + the health points of the animal + hidden : bool + the hiding status of the animal + """ + + alive = list() + + def __init__( + self, + name: str, + health: int = 100, + hidden: bool = False + ) -> None: + self.name = name + self.health = health + self.hidden = hidden + if self.health > 0: + Animal.alive.append(self) + + def __repr__(self) -> str: + return (f"" + f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}" + ) + + +class Herbivore(Animal): + """ + Represents a Herbivore animal. + + Attributes: + ---------- + name : str + the name of the animal + health : int + the health points of the animal + hidden : bool + the hiding status of the animal + + Methods: + ------- + hide() + Switches the "hidden" + attribute of the animal to the opposite value. + """ + + def hide(self) -> None: + """ + Switches the "hidden" attribute of the animal to the opposite value. + :return: None + """ + self.hidden = not self.hidden + + +class Carnivore(Animal): + """ + Represents a Carnivore animal. + + Attributes: + ---------- + name : str + the name of the animal + health : int + the health points of the animal + hidden : bool + the hiding status of the animal + + Methods: + ------- + bite() + Takes a Herbivore instance and decreases its "health" + attribute by 50 pts. + """ + + @staticmethod + def bite(herbivore: Herbivore) -> None: + """ + Takes a Herbivore instance and decreases its "health" + attribute by 50 pts. + Removes the Herbivore instance out of "Animal.alive" + class attribute if its "health" <= 0. + The method does not work against another Carnivore instance, + or if the Herbivore's "hidden" attribute is currently True. + :param herbivore: Herbivore, mandatory + A Herbivore instance + :return: None + """ + if isinstance(herbivore, Herbivore) and not herbivore.hidden: + herbivore.health -= 50 + if herbivore.health <= 0: + Animal.alive.remove(herbivore) From c347d0928410b6f30a567527314ff2af65c8902e Mon Sep 17 00:00:00 2001 From: Denys Kibalenko Date: Tue, 3 Oct 2023 14:40:06 +0200 Subject: [PATCH 2/2] Solution Fix test condition --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 818c54d5..2345b452 100644 --- a/app/main.py +++ b/app/main.py @@ -97,5 +97,5 @@ class attribute if its "health" <= 0. """ if isinstance(herbivore, Herbivore) and not herbivore.hidden: herbivore.health -= 50 - if herbivore.health <= 0: + if isinstance(herbivore, Herbivore) and herbivore.health <= 0: Animal.alive.remove(herbivore)