From 53d399c2f0effe4a5fcc94eafa606cd3a334c7e0 Mon Sep 17 00:00:00 2001 From: Illya Kovaliuk Date: Sun, 29 Dec 2024 22:32:11 +0200 Subject: [PATCH] Solutionn --- app/main.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..2bf2845c9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,78 @@ +from __future__ import annotations + + class Distance: - # Write your code here - pass + def __init__(self, km: 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: Distance | float) -> Distance: + if isinstance(other, Distance): + return Distance(self.km + other.km) + elif isinstance(other, (int, float)): + return Distance(self.km + other) + return NotImplemented + + def __iadd__(self, other: Distance | float) -> Distance: + if isinstance(other, Distance): + self.km += other.km + elif isinstance(other, (int, float)): + self.km += other + return self + + def __mul__(self, factor: float) -> Distance: + if isinstance(factor, (int, float)): + return Distance(self.km * factor) + return NotImplemented + + def __truediv__(self, divisor: float) -> Distance: + if isinstance(divisor, (int, float)) and divisor != 0: + return Distance(round(self.km / divisor, 2)) + return NotImplemented + + def __lt__(self, other: Distance | float) -> bool: + if isinstance(other, Distance): + return self.km < other.km + elif isinstance(other, (int, float)): + return self.km < other + return NotImplemented + + def __le__(self, other: Distance | float) -> bool: + if isinstance(other, Distance): + return self.km <= other.km + elif isinstance(other, (int, float)): + return self.km <= other + return NotImplemented + + def __eq__(self, other: Distance | float) -> bool: + if isinstance(other, Distance): + return self.km == other.km + elif isinstance(other, (int, float)): + return self.km == other + return NotImplemented + + def __ne__(self, other: Distance | float) -> bool: + if isinstance(other, Distance): + return self.km != other.km + elif isinstance(other, (int, float)): + return self.km != other + return NotImplemented + + def __gt__(self, other: Distance | float) -> bool: + if isinstance(other, Distance): + return self.km > other.km + elif isinstance(other, (int, float)): + return self.km > other + return NotImplemented + + def __ge__(self, other: Distance | float) -> bool: + if isinstance(other, Distance): + return self.km >= other.km + elif isinstance(other, (int, float)): + return self.km >= other + return NotImplemented