From 6447a77f9af2c607f91236fe0fb23773879aeeac Mon Sep 17 00:00:00 2001 From: Vik Date: Mon, 13 Jan 2025 19:38:10 +0200 Subject: [PATCH 1/2] 'Solution' --- app/main.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..2b3124af1 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,45 @@ +from __future__ import annotations + + 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 | int | float) -> Distance: + if isinstance(other, Distance): + return Distance(self.km + other.km) + return Distance(self.km + other) + + def __iadd__(self, other: Distance | int | float) -> Distance: + if isinstance(other, Distance): + self.km += other.km + else: + self.km = self.km + other + return self + + def __mul__(self, other: Distance | int | float) -> Distance: + return Distance(self.km * other) + + def __truediv__(self, other: Distance | int | float) -> Distance: + return Distance(round(self.km / other, 2)) + + def __lt__(self, other: Distance) -> bool: + return self.km < other + + def __gt__(self, other: Distance | int | float) -> bool: + return self.km > other + + def __eq__(self, other: Distance) -> bool: + return self.km == other + + def __le__(self, other: Distance) -> bool: + return self.km <= other + + def __ge__(self, other: Distance) -> bool: + return self.km >= other From 53eacf8fdb6822c7a1f4055ba484acdc903c2d91 Mon Sep 17 00:00:00 2001 From: Vik Date: Mon, 13 Jan 2025 21:03:30 +0200 Subject: [PATCH 2/2] 'Solution1' --- app/main.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 2b3124af1..b9371e2ac 100644 --- a/app/main.py +++ b/app/main.py @@ -23,23 +23,33 @@ def __iadd__(self, other: Distance | int | float) -> Distance: self.km = self.km + other return self - def __mul__(self, other: Distance | int | float) -> Distance: + def __mul__(self, other: int | float) -> Distance: return Distance(self.km * other) - def __truediv__(self, other: Distance | int | float) -> Distance: + def __truediv__(self, other: int | float) -> Distance: return Distance(round(self.km / other, 2)) - def __lt__(self, other: Distance) -> bool: + def __lt__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km < other.km return self.km < other def __gt__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km > other.km return self.km > other - def __eq__(self, other: Distance) -> bool: + def __eq__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km == other.km return self.km == other - def __le__(self, other: Distance) -> bool: + def __le__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km <= other.km return self.km <= other - def __ge__(self, other: Distance) -> bool: + def __ge__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km >= other.km return self.km >= other