From c6e0b45461fcf381ff48c204820a8d76cd7852eb Mon Sep 17 00:00:00 2001 From: Vladimir Dolhyi Date: Sat, 6 Jan 2024 12:49:07 +0200 Subject: [PATCH 1/2] Solution --- app/main.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..c0eeeb6af 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,55 @@ +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) -> Distance: + if isinstance(other, Distance): + return Distance(self.km + other.km) + return Distance(self.km + other) + + def __iadd__(self, other: Distance | int) -> Distance: + if isinstance(other, Distance): + self.km += other.km + else: + self.km += other + return self + + def __mul__(self, other: int) -> Distance: + return Distance(self.km * other) + + def __truediv__(self, other: int) -> Distance: + return Distance(round(self.km / other, 2)) + + def __lt__(self, other: Distance | int) -> bool: + if isinstance(other, Distance): + return self.km < other.km + return self.km < other + + def __gt__(self, other: Distance | int) -> bool: + if isinstance(other, Distance): + return self.km > other.km + return self.km > other + + 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 | int) -> bool: + if isinstance(other, Distance): + return self.km <= other.km + return self.km <= other + + def __ge__(self, other: Distance | int) -> bool: + if isinstance(other, Distance): + return self.km >= other.km + return self.km >= other From 885380e5555495add68b3e48aecfdf5d31ade575 Mon Sep 17 00:00:00 2001 From: Vladimir Dolhyi Date: Sat, 6 Jan 2024 14:30:18 +0200 Subject: [PATCH 2/2] add float + simplify --- app/main.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/app/main.py b/app/main.py index c0eeeb6af..32f190e51 100644 --- a/app/main.py +++ b/app/main.py @@ -11,30 +11,30 @@ def __str__(self) -> str: def __repr__(self) -> str: return f"Distance(km={self.km})" - def __add__(self, other: Distance | int) -> Distance: + 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) -> Distance: + def __iadd__(self, other: Distance | int | float) -> Distance: if isinstance(other, Distance): self.km += other.km else: self.km += other return self - def __mul__(self, other: int) -> Distance: + def __mul__(self, other: int | float) -> Distance: return Distance(self.km * other) - def __truediv__(self, other: int) -> Distance: + def __truediv__(self, other: int | float) -> Distance: return Distance(round(self.km / other, 2)) - def __lt__(self, other: Distance | int) -> 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) -> bool: + def __gt__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km > other.km return self.km > other @@ -44,12 +44,8 @@ def __eq__(self, other: Distance | int | float) -> bool: return self.km == other.km return self.km == other - def __le__(self, other: Distance | int) -> bool: - if isinstance(other, Distance): - return self.km <= other.km - return self.km <= other + def __le__(self, other: Distance | int | float) -> bool: + return self == other or self < other - def __ge__(self, other: Distance | int) -> bool: - if isinstance(other, Distance): - return self.km >= other.km - return self.km >= other + def __ge__(self, other: Distance | int | float) -> bool: + return self == other or self > other