From 3d3d7d0b12d805fbab08f71817edb69452de7b5d Mon Sep 17 00:00:00 2001 From: Oleksandr Barabanov Date: Sun, 8 Dec 2024 22:55:01 +0200 Subject: [PATCH 1/4] Solution --- .flake8 | 2 +- app/main.py | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/.flake8 b/.flake8 index d7459204..3f5e06ff 100644 --- a/.flake8 +++ b/.flake8 @@ -4,4 +4,4 @@ ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818 max-line-length = 79 max-complexity = 18 select = B,C,E,F,W,T4,B9,ANN,Q0,N8,VNE -exclude = venv, tests +exclude = venv, tests, .venv diff --git a/app/main.py b/app/main.py index fa56336e..86392540 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,41 @@ -# write your code here +class Animal: + alive = [] + + def __init__(self, + name: str, + health: int = 100 + ) -> None: + self.name = name + self.health = health + self.hidden = False + Animal.alive.append(self) + + def __repr__(self) -> str: + return (f"{{Name: {self.name}, Health: {self.health}, \n" + f"Hidden: {self.hidden}}}") + + @classmethod + def remove_dead(cls) -> None: + cls.alive = [animal for animal in cls.alive if animal.health > 0] + + @classmethod + def __str__(cls) -> str: + return str([repr(animal) for animal in cls.alive]) + + @classmethod + def print_alive(cls) -> list: + return cls.alive + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + def bite(self, other: "Animal") -> None: + if isinstance(other, Herbivore) and not other.hidden: + other.health -= 50 + if other.health <= 0: + other.health = 0 + Animal.remove_dead() From 74a7f982a1f06894ee0db9e52c90063663e1ccaf Mon Sep 17 00:00:00 2001 From: Oleksandr Barabanov Date: Sun, 8 Dec 2024 23:46:25 +0200 Subject: [PATCH 2/4] Solution --- app/main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 86392540..96351406 100644 --- a/app/main.py +++ b/app/main.py @@ -11,7 +11,8 @@ def __init__(self, Animal.alive.append(self) def __repr__(self) -> str: - return (f"{{Name: {self.name}, Health: {self.health}, \n" + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " f"Hidden: {self.hidden}}}") @classmethod @@ -33,7 +34,8 @@ def hide(self) -> None: class Carnivore(Animal): - def bite(self, other: "Animal") -> None: + @staticmethod + def bite(other: "Animal") -> None: if isinstance(other, Herbivore) and not other.hidden: other.health -= 50 if other.health <= 0: From 282adfc425049ba03aa9df83ffb4b7f61f355f85 Mon Sep 17 00:00:00 2001 From: Oleksandr Barabanov Date: Tue, 10 Dec 2024 13:09:40 +0200 Subject: [PATCH 3/4] Updated --- app/main.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/app/main.py b/app/main.py index 96351406..f06cc509 100644 --- a/app/main.py +++ b/app/main.py @@ -3,8 +3,7 @@ class Animal: def __init__(self, name: str, - health: int = 100 - ) -> None: + health: int = 100) -> None: self.name = name self.health = health self.hidden = False @@ -20,11 +19,11 @@ def remove_dead(cls) -> None: cls.alive = [animal for animal in cls.alive if animal.health > 0] @classmethod - def __str__(cls) -> str: + def get_alive_repr(cls) -> str: return str([repr(animal) for animal in cls.alive]) @classmethod - def print_alive(cls) -> list: + def get_alive(cls) -> list: return cls.alive @@ -35,9 +34,9 @@ def hide(self) -> None: class Carnivore(Animal): @staticmethod - def bite(other: "Animal") -> None: - if isinstance(other, Herbivore) and not other.hidden: - other.health -= 50 - if other.health <= 0: - other.health = 0 - Animal.remove_dead() + def bite(target: Animal) -> None: + if isinstance(target, Herbivore) and not target.hidden: + target.health -= 50 + if target.health <= 0: + target.health = 0 + Animal.alive.remove(target) From 9a19eef977dfd85c3274e77eb2e3647c16e4d38c Mon Sep 17 00:00:00 2001 From: Oleksandr Barabanov Date: Tue, 10 Dec 2024 21:00:53 +0200 Subject: [PATCH 4/4] Updated --- app/main.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/app/main.py b/app/main.py index f06cc509..4099c4ff 100644 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,11 @@ class Animal: alive = [] - def __init__(self, - name: str, - health: int = 100) -> None: + def __init__( + self, + name: str, + health: int = 100 + ) -> None: self.name = name self.health = health self.hidden = False @@ -14,18 +16,6 @@ def __repr__(self) -> str: f"Health: {self.health}, " f"Hidden: {self.hidden}}}") - @classmethod - def remove_dead(cls) -> None: - cls.alive = [animal for animal in cls.alive if animal.health > 0] - - @classmethod - def get_alive_repr(cls) -> str: - return str([repr(animal) for animal in cls.alive]) - - @classmethod - def get_alive(cls) -> list: - return cls.alive - class Herbivore(Animal): def hide(self) -> None: @@ -38,5 +28,4 @@ def bite(target: Animal) -> None: if isinstance(target, Herbivore) and not target.hidden: target.health -= 50 if target.health <= 0: - target.health = 0 Animal.alive.remove(target)