From 133d418e6f820b248134905b3de2abd72bf2c79b Mon Sep 17 00:00:00 2001 From: Adrianna Regulska Date: Sun, 15 Dec 2024 16:42:11 +0100 Subject: [PATCH] Solution --- app/main.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..bdbc8b720 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,72 @@ class Distance: - # Write your code here - pass + def __init__(self, km: int | float) -> 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: object) -> "Distance": + if isinstance(other, Distance): + return Distance(self.km + other.km) + if isinstance(other, (float, int)): + return Distance(self.km + other) + return NotImplemented + + def __iadd__(self, other: object) -> "Distance": + if isinstance(other, Distance): + self.km += other.km + return self + if isinstance(other, (float, int)): + self.km += other + return self + return NotImplemented + + def __mul__(self, other: object) -> "Distance": + if isinstance(other, (int, float)): + return Distance(self.km * other) + return NotImplemented + + def __truediv__(self, other: object) -> "Distance": + if isinstance(other, (int, float)): + if other == 0: + raise ValueError("You can't divide by zero!") + return Distance(round((self.km / other), 2)) + return NotImplemented + + def __lt__(self, other: object) -> bool: + if isinstance(other, Distance): + return self.km < other.km + if isinstance(other, (float, int)): + return self.km < other + return NotImplemented + + def __gt__(self, other: object) -> bool: + if isinstance(other, Distance): + return self.km > other.km + if isinstance(other, (float, int)): + return self.km > other + return NotImplemented + + def __eq__(self, other: object) -> bool: + if isinstance(other, Distance): + return self.km == other.km + if isinstance(other, (float, int)): + return self.km == other + return NotImplemented + + def __le__(self, other: object) -> bool: + if isinstance(other, Distance): + return self.km <= other.km + if isinstance(other, (float, int)): + return self.km <= other + return NotImplemented + + def __ge__(self, other: object) -> bool: + if isinstance(other, Distance): + return self.km >= other.km + if isinstance(other, (float, int)): + return self.km >= other + return NotImplemented