From 1e742fb44ac5a9945b0c40fe499b661cd6418c64 Mon Sep 17 00:00:00 2001 From: anastainska Date: Sat, 11 Jan 2025 14:40:53 +0200 Subject: [PATCH] Solution --- app/main.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..28dbaee06 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,63 @@ 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: "Distance") -> "Distance": + if isinstance(other, Distance): + return Distance(self.km + other.km) + + if isinstance(other, (int, float)): + return Distance(self.km + other) + + def __iadd__(self, other: "Distance") -> "Distance": + if isinstance(other, Distance): + self.km += other.km + elif isinstance(other, (int, float)): + self.km += other + return self + + def __mul__(self, other: "Distance") -> "Distance": + if isinstance(other, (int, float)): + return Distance(self.km * other) + + def __truediv__(self, other: "Distance") -> "Distance": + if isinstance(other, (int, float)): + return Distance(round(self.km / other, 2)) + elif other == 0: + raise ZeroDivisionError("Cannot divide by zero.") + + def __lt__(self, other: "Distance") -> bool: + if isinstance(other, Distance): + return self.km < other.km + elif isinstance(other, (float, int)): + return self.km < other + + def __gt__(self, other: "Distance") -> bool: + if isinstance(other, Distance): + return self.km > other.km + elif isinstance(other, (float, int)): + return self.km > other + + def __eq__(self, other: "Distance") -> bool: + if isinstance(other, Distance): + return self.km == other.km + elif isinstance(other, (float, int)): + return self.km == other + + def __le__(self, other: "Distance") -> bool: + if isinstance(other, Distance): + return self.km <= other.km + elif isinstance(other, (float, int)): + return self.km <= other + + def __ge__(self, other: "Distance") -> bool: + if isinstance(other, Distance): + return self.km >= other.km + elif isinstance(other, (float, int)): + return self.km >= other