From 1110934bdd1375045a2f559d278ed2a29cd1bac6 Mon Sep 17 00:00:00 2001 From: Denis Komandyr Date: Sat, 7 Oct 2023 20:03:18 +0300 Subject: [PATCH 1/4] Animals Battle Royal --- app/main.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..6d82efd5 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,35 @@ -# 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 __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 = not self.hidden + + +class Carnivore(Animal): + @staticmethod + def bite(target: Herbivore) -> None: + if not isinstance(target, Carnivore) and not target.hidden: + if target.health > 50: + target.health -= 50 + elif target.health <= 50: + target.health = 0 + Animal.alive.pop(Animal.alive.index(target)) From 6f8b53ad34454aae57d261056d6d08db5108a057 Mon Sep 17 00:00:00 2001 From: Denis Komandyr Date: Tue, 10 Oct 2023 00:57:32 +0300 Subject: [PATCH 2/4] bite conditions simplified --- app/main.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 6d82efd5..b9951a72 100644 --- a/app/main.py +++ b/app/main.py @@ -28,8 +28,6 @@ class Carnivore(Animal): @staticmethod def bite(target: Herbivore) -> None: if not isinstance(target, Carnivore) and not target.hidden: - if target.health > 50: - target.health -= 50 - elif target.health <= 50: - target.health = 0 + target.health -= 50 + if target.health <= 0: Animal.alive.pop(Animal.alive.index(target)) From bce780e6b5c3b76a7575c3c4cafd7016fbeda037 Mon Sep 17 00:00:00 2001 From: Denis Komandyr Date: Thu, 12 Oct 2023 02:40:32 +0300 Subject: [PATCH 3/4] added brackets --- app/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index b9951a72..506896a9 100644 --- a/app/main.py +++ b/app/main.py @@ -14,9 +14,9 @@ def __init__( Animal.alive.append(self) def __repr__(self) -> str: - return f"{{Name: {self.name}, " \ - f"Health: {self.health}, " \ - f"Hidden: {self.hidden}}}" + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") class Herbivore(Animal): From b60b28c3338f3addf053569f28a603ae12d71e30 Mon Sep 17 00:00:00 2001 From: Denis Komandyr Date: Sat, 14 Oct 2023 00:57:04 +0300 Subject: [PATCH 4/4] fixed type annotations --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 506896a9..ec7b79fe 100644 --- a/app/main.py +++ b/app/main.py @@ -26,8 +26,8 @@ def hide(self) -> None: class Carnivore(Animal): @staticmethod - def bite(target: Herbivore) -> None: + def bite(target: Animal) -> None: if not isinstance(target, Carnivore) and not target.hidden: target.health -= 50 if target.health <= 0: - Animal.alive.pop(Animal.alive.index(target)) + Animal.alive.remove(target)