From 5a13c0c5d3afc4838b6a1dde6b1f4c5a4d3b82db Mon Sep 17 00:00:00 2001 From: Ivan Revko Date: Fri, 31 Jan 2025 11:36:50 +0200 Subject: [PATCH 1/4] Solution --- app/main.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..780d6b8da 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,45 @@ class Distance: - # Write your code here - pass + def __init__(self, km: int) -> None: + self.km = km + + def __str__(self) -> str: + return f"Distance: {self.km} kilometers." + + def __repr__(self) -> str: + return f"Distance(km={self.km})" + + def __add__(self, other: (int, float)) -> "Distance": + if isinstance(other, Distance): + return Distance(self.km + other.km) + elif isinstance(other, (int, float)): + return Distance(self.km + other) + + def __iadd__(self, other: (int, float)) -> (int, float): + if isinstance(other, Distance): + self.km += other.km + return self + elif isinstance(other, (int, float)): + self.km += other + return self + + def __mul__(self, other: (int, float)) -> "Distance": + if isinstance(other, (int, float)): + return Distance(self.km * other) + + def __truediv__(self, other: (int, float)) -> "Distance": + return Distance(round(self.km / other, 2)) + + def __lt__(self, other: (int, float)) -> bool: + return self.km < other + + def __gt__(self, other: (int, float)) -> bool: + return self.km > other + + def __eq__(self, other: (int, float)) -> bool: + return self.km == other + + def __le__(self, other: (int, float)) -> bool: + return self.km <= other + + def __ge__(self, other: int) -> bool: + return self.km >= other From 547780ac1af84a03fdefb5cc69b24d67f2fc4900 Mon Sep 17 00:00:00 2001 From: Ivan Revko Date: Fri, 31 Jan 2025 11:47:01 +0200 Subject: [PATCH 2/4] Fix the issues --- app/main.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 780d6b8da..03b964bf9 100644 --- a/app/main.py +++ b/app/main.py @@ -14,7 +14,7 @@ def __add__(self, other: (int, float)) -> "Distance": elif isinstance(other, (int, float)): return Distance(self.km + other) - def __iadd__(self, other: (int, float)) -> (int, float): + def __iadd__(self, other: (int, float)) -> "Distance": if isinstance(other, Distance): self.km += other.km return self @@ -30,16 +30,31 @@ def __truediv__(self, other: (int, float)) -> "Distance": return Distance(round(self.km / other, 2)) def __lt__(self, other: (int, float)) -> bool: - return self.km < other + if isinstance(other, Distance): + return self.km < other.km + elif isinstance(other, (int, float)): + return self.km < other def __gt__(self, other: (int, float)) -> bool: - return self.km > other + if isinstance(other, Distance): + return self.km > other.km + elif isinstance(other, (int, float)): + return self.km > other def __eq__(self, other: (int, float)) -> bool: - return self.km == other + if isinstance(other, Distance): + return self.km == other.km + elif isinstance(other, (int, float)): + return self.km == other def __le__(self, other: (int, float)) -> bool: - return self.km <= other + if isinstance(other, Distance): + return self.km <= other.km + elif isinstance(other, (int, float)): + return self.km <= other def __ge__(self, other: int) -> bool: - return self.km >= other + if isinstance(other, Distance): + return self.km >= other.km + elif isinstance(other, (int, float)): + return self.km >= other From 93b69e8b6670e4cc54a9f031e9503341fcfd196a Mon Sep 17 00:00:00 2001 From: Ivan Revko Date: Fri, 31 Jan 2025 12:01:05 +0200 Subject: [PATCH 3/4] Corrected types for __ge__ method --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 03b964bf9..6b86c662f 100644 --- a/app/main.py +++ b/app/main.py @@ -53,7 +53,7 @@ def __le__(self, other: (int, float)) -> bool: elif isinstance(other, (int, float)): return self.km <= other - def __ge__(self, other: int) -> bool: + def __ge__(self, other: (int, float)) -> bool: if isinstance(other, Distance): return self.km >= other.km elif isinstance(other, (int, float)): From 708b586593147255419479b90b5c878785ab6704 Mon Sep 17 00:00:00 2001 From: Ivan Revko Date: Fri, 31 Jan 2025 12:42:32 +0200 Subject: [PATCH 4/4] Shorter code --- app/main.py | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/app/main.py b/app/main.py index 6b86c662f..4f9fa5b1d 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,6 @@ +from __future__ import annotations + + class Distance: def __init__(self, km: int) -> None: self.km = km @@ -8,53 +11,45 @@ def __str__(self) -> str: def __repr__(self) -> str: return f"Distance(km={self.km})" - def __add__(self, other: (int, float)) -> "Distance": + def __add__(self, other: int | Distance) -> Distance: if isinstance(other, Distance): return Distance(self.km + other.km) - elif isinstance(other, (int, float)): - return Distance(self.km + other) + return Distance(self.km + other) - def __iadd__(self, other: (int, float)) -> "Distance": + def __iadd__(self, other: int | Distance) -> Distance: if isinstance(other, Distance): self.km += other.km return self - elif isinstance(other, (int, float)): - self.km += other - return self + self.km += other + return self - def __mul__(self, other: (int, float)) -> "Distance": - if isinstance(other, (int, float)): - return Distance(self.km * other) + def __mul__(self, other: int) -> Distance: + return Distance(self.km * other) - def __truediv__(self, other: (int, float)) -> "Distance": + def __truediv__(self, other: (int, float)) -> Distance: return Distance(round(self.km / other, 2)) def __lt__(self, other: (int, float)) -> bool: if isinstance(other, Distance): return self.km < other.km - elif isinstance(other, (int, float)): - return self.km < other + return self.km < other - def __gt__(self, other: (int, float)) -> bool: + def __gt__(self, other: int | Distance) -> bool: if isinstance(other, Distance): return self.km > other.km - elif isinstance(other, (int, float)): - return self.km > other + return self.km > other - def __eq__(self, other: (int, float)) -> bool: + def __eq__(self, other: int | Distance) -> bool: if isinstance(other, Distance): return self.km == other.km - elif isinstance(other, (int, float)): - return self.km == other + return self.km == other - def __le__(self, other: (int, float)) -> bool: + def __le__(self, other: int | Distance) -> bool: if isinstance(other, Distance): return self.km <= other.km - elif isinstance(other, (int, float)): - return self.km <= other + return self.km <= other - def __ge__(self, other: (int, float)) -> bool: + def __ge__(self, other: int | Distance) -> bool: if isinstance(other, Distance): return self.km >= other.km - elif isinstance(other, (int, float)): - return self.km >= other + return self.km >= other