From 4a917c6f72a9cdfc8be253baf7c1869adbfa4163 Mon Sep 17 00:00:00 2001 From: git_practic Date: Fri, 6 Dec 2024 18:36:40 +0200 Subject: [PATCH 01/10] 'Solution' --- app/main.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..5c7540b8 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,46 @@ -# write your code here +from __future__ import annotations +from typing import Union + + +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 __str__(self) -> str: + + return (f"Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}") + + def __repr__(self) -> str: + + return f"{{{self}}}" + + +class Herbivore(Animal): + + def hide(self) -> None: + if not self.hidden: + self.hidden = True + return + self.hidden = False + + +class Carnivore(Animal): + + @staticmethod + def bite(other: Union[Carnivore, Herbivore]) -> Union[str, None]: + if isinstance(other, Carnivore): + return "Carnivore won't bit Carnivore" + if isinstance(other, Herbivore): + if other.hidden: + return "Carnivore won't be able to bite hidden Herbivore" + if other.health > 0: + other.health -= 50 + if other.health <= 0: + Animal.alive.pop(Animal.alive.index(other)) From 2569fd6c166819f3e77ab7e0c3a83c5a05e7606d Mon Sep 17 00:00:00 2001 From: git_practic Date: Mon, 9 Dec 2024 12:05:50 +0200 Subject: [PATCH 02/10] 'simplification-bit-method' --- app/main.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index 5c7540b8..564bb41e 100644 --- a/app/main.py +++ b/app/main.py @@ -7,8 +7,8 @@ class Animal: def __init__(self, name: str, health: int = 100) -> None: self.name = name - self.health = health self.hidden = False + self.health = health Animal.alive.append(self) def __str__(self) -> str: @@ -37,10 +37,9 @@ class Carnivore(Animal): def bite(other: Union[Carnivore, Herbivore]) -> Union[str, None]: if isinstance(other, Carnivore): return "Carnivore won't bit Carnivore" - if isinstance(other, Herbivore): - if other.hidden: - return "Carnivore won't be able to bite hidden Herbivore" - if other.health > 0: - other.health -= 50 - if other.health <= 0: - Animal.alive.pop(Animal.alive.index(other)) + if isinstance(other, Herbivore) and other.hidden: + return "Carnivore won't be able to bite hidden Herbivore" + if other.health > 0: + other.health -= 50 + if other.health <= 0: + Animal.alive.pop(Animal.alive.index(other)) From 36c37c3b92d6886c72aa30ac5c5d5a1b062753b5 Mon Sep 17 00:00:00 2001 From: git_practic Date: Tue, 10 Dec 2024 08:52:27 +0200 Subject: [PATCH 03/10] 'rewrite-condition-into-Herbovire-hide-method' --- app/main.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 564bb41e..52eeca4c 100644 --- a/app/main.py +++ b/app/main.py @@ -25,10 +25,7 @@ def __repr__(self) -> str: class Herbivore(Animal): def hide(self) -> None: - if not self.hidden: - self.hidden = True - return - self.hidden = False + self.hidden = not self.hidden class Carnivore(Animal): From f732fa551f5ad512a8a8d3a9a87ea1aae62107d0 Mon Sep 17 00:00:00 2001 From: git_practic Date: Tue, 10 Dec 2024 09:00:18 +0200 Subject: [PATCH 04/10] 'explicity-returntype-into-Carnovire-bite-method-and-del-unusefull-check' --- app/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 52eeca4c..4289e016 100644 --- a/app/main.py +++ b/app/main.py @@ -31,12 +31,11 @@ def hide(self) -> None: class Carnivore(Animal): @staticmethod - def bite(other: Union[Carnivore, Herbivore]) -> Union[str, None]: + def bite(other: Union[Carnivore, Herbivore]) -> str | None: if isinstance(other, Carnivore): return "Carnivore won't bit Carnivore" if isinstance(other, Herbivore) and other.hidden: return "Carnivore won't be able to bite hidden Herbivore" - if other.health > 0: - other.health -= 50 + other.health -= 50 if other.health <= 0: Animal.alive.pop(Animal.alive.index(other)) From 2bd5d3b39e2a5e25298bc5c430decfbac5a1e360 Mon Sep 17 00:00:00 2001 From: git_practic Date: Tue, 10 Dec 2024 09:16:04 +0200 Subject: [PATCH 05/10] 'del_str_method' --- app/main.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index 4289e016..f62e01d5 100644 --- a/app/main.py +++ b/app/main.py @@ -11,15 +11,11 @@ def __init__(self, name: str, health: int = 100) -> None: self.health = health Animal.alive.append(self) - def __str__(self) -> str: - - return (f"Name: {self.name}, " - f"Health: {self.health}, " - f"Hidden: {self.hidden}") - def __repr__(self) -> str: - - return f"{{{self}}}" + return (f"{{" + f"Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") class Herbivore(Animal): From e156b7eb477c6741619e85f565c96316f9b1c706 Mon Sep 17 00:00:00 2001 From: git_practic Date: Tue, 10 Dec 2024 09:17:57 +0200 Subject: [PATCH 06/10] 'del-return-text-into-Carnovire-bite-method-and-del-return-type-str' --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index f62e01d5..335fea18 100644 --- a/app/main.py +++ b/app/main.py @@ -29,9 +29,9 @@ class Carnivore(Animal): @staticmethod def bite(other: Union[Carnivore, Herbivore]) -> str | None: if isinstance(other, Carnivore): - return "Carnivore won't bit Carnivore" + return if isinstance(other, Herbivore) and other.hidden: - return "Carnivore won't be able to bite hidden Herbivore" + return other.health -= 50 if other.health <= 0: Animal.alive.pop(Animal.alive.index(other)) From 8dfd528592d010a8beef29335b787933c9458082 Mon Sep 17 00:00:00 2001 From: git_practic Date: Tue, 10 Dec 2024 09:18:09 +0200 Subject: [PATCH 07/10] 'del-return-text-into-Carnovire-bite-method-and-del-return-type-str' --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 335fea18..5d9c2f6b 100644 --- a/app/main.py +++ b/app/main.py @@ -27,7 +27,7 @@ def hide(self) -> None: class Carnivore(Animal): @staticmethod - def bite(other: Union[Carnivore, Herbivore]) -> str | None: + def bite(other: Union[Carnivore, Herbivore]) -> None: if isinstance(other, Carnivore): return if isinstance(other, Herbivore) and other.hidden: From 005e21ac03b2535605d8e04f2c1fa3f1688c7274 Mon Sep 17 00:00:00 2001 From: git_practic Date: Tue, 10 Dec 2024 20:45:58 +0200 Subject: [PATCH 08/10] 'simplify-bit-method' --- app/main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/main.py b/app/main.py index 5d9c2f6b..1af3233a 100644 --- a/app/main.py +++ b/app/main.py @@ -28,8 +28,6 @@ class Carnivore(Animal): @staticmethod def bite(other: Union[Carnivore, Herbivore]) -> None: - if isinstance(other, Carnivore): - return if isinstance(other, Herbivore) and other.hidden: return other.health -= 50 From dd57fcf17ca7ebfcbc8eb83bf486feb7476649fc Mon Sep 17 00:00:00 2001 From: git_practic Date: Tue, 10 Dec 2024 21:04:34 +0200 Subject: [PATCH 09/10] 'deleting-unusefull-class-type-annotations-into-bite-method-and-simplify-this-one' --- app/main.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/main.py b/app/main.py index 1af3233a..2effcb78 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,4 @@ from __future__ import annotations -from typing import Union class Animal: @@ -27,9 +26,8 @@ def hide(self) -> None: class Carnivore(Animal): @staticmethod - def bite(other: Union[Carnivore, Herbivore]) -> None: - if isinstance(other, Herbivore) and other.hidden: - return - other.health -= 50 - if other.health <= 0: - Animal.alive.pop(Animal.alive.index(other)) + def bite(other: Animal) -> None: + if isinstance(other, Herbivore) and not other.hidden: + other.health -= 50 + if other.health <= 0: + Animal.alive.remove(other) From e90472ebbea9145672d1203b81d0b88955c23725 Mon Sep 17 00:00:00 2001 From: git_practic Date: Wed, 11 Dec 2024 19:46:09 +0200 Subject: [PATCH 10/10] 'refactor-_repr_-method' --- app/main.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 2effcb78..86105477 100644 --- a/app/main.py +++ b/app/main.py @@ -11,10 +11,13 @@ def __init__(self, name: str, health: int = 100) -> None: Animal.alive.append(self) def __repr__(self) -> str: - return (f"{{" - f"Name: {self.name}, " - f"Health: {self.health}, " - f"Hidden: {self.hidden}}}") + return ( + f"{{" + f"Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}" + f"}}" + ) class Herbivore(Animal):