From 43971d79391f8d275ed7dded305300c2578d9cb8 Mon Sep 17 00:00:00 2001 From: Aleksandr Paslawski Date: Wed, 1 Nov 2023 21:18:59 +0100 Subject: [PATCH 1/3] Task done --- app/main.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..995365595 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,56 @@ +from __future__ import annotations + + class Distance: - # Write your code here - pass + + def __init__(self, km: int) -> None: + self.km = km + + def __repr__(self) -> str: + return f"Distance(km={self.km})" + + def __str__(self) -> str: + return f"Distance: {self.km} kilometers." + + def __add__(self, second: Distance | int | float) -> Distance: + if isinstance(second, Distance): + return Distance(km=self.km + second.km) + return Distance(km=self.km + second) + + def __iadd__(self, second: Distance | int | float) -> Distance: + if isinstance(second, Distance): + self.km = self.km + second.km + return self + self.km = self.km + second + return self + + def __mul__(self, second: int | float) -> Distance: + return Distance(km=self.km * second) + + def __truediv__(self, second: int | float) -> Distance: + return Distance(km=round(self.km / second, 2)) + + def __lt__(self, second: Distance | int | float) -> bool: + if isinstance(second, Distance): + return self.km < second.km + return self.km < second + + def __gt__(self, second: Distance | int | float) -> bool: + if isinstance(second, Distance): + return self.km > second.km + return self.km > second + + def __eq__(self, second: Distance | int | float) -> bool: + if isinstance(second, Distance): + return self.km == second.km + return self.km == second + + def __le__(self, second: Distance | int | float) -> bool: + if isinstance(second, Distance): + return self.km <= second.km + return self.km <= second + + def __ge__(self, second: Distance | int | float) -> bool: + if isinstance(second, Distance): + return self.km >= second.km + return self.km >= second From 86e75b214fecb45adea86a4d04fdfc203ed7cffd Mon Sep 17 00:00:00 2001 From: Aleksandr Paslawski Date: Thu, 2 Nov 2023 12:18:18 +0100 Subject: [PATCH 2/3] Type annotation added --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 995365595..2bde13f48 100644 --- a/app/main.py +++ b/app/main.py @@ -3,7 +3,7 @@ class Distance: - def __init__(self, km: int) -> None: + def __init__(self, km: int | float) -> None: self.km = km def __repr__(self) -> str: From 08715c10fad9c441d2d959f826d76f96d1dfa604 Mon Sep 17 00:00:00 2001 From: Aleksandr Paslawski Date: Thu, 2 Nov 2023 13:32:15 +0100 Subject: [PATCH 3/3] one return was made --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 2bde13f48..a0acf7fef 100644 --- a/app/main.py +++ b/app/main.py @@ -20,8 +20,8 @@ def __add__(self, second: Distance | int | float) -> Distance: def __iadd__(self, second: Distance | int | float) -> Distance: if isinstance(second, Distance): self.km = self.km + second.km - return self - self.km = self.km + second + else: + self.km = self.km + second return self def __mul__(self, second: int | float) -> Distance: