From 8cd3fc718be657fa707fdf182cc8ed0da67dfca0 Mon Sep 17 00:00:00 2001 From: Ivan Fursyk Date: Thu, 3 Oct 2024 15:18:32 +0300 Subject: [PATCH 1/2] Solution --- app/main.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa341..14292803 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,61 @@ 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: int) -> "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: int) -> "Distance": + if isinstance(other, Distance): + self.km += other.km + elif isinstance(other, (int, float)): + self.km += other + return self + + def __mul__(self, other: int) -> "Distance": + if isinstance(other, (int, float)): + return Distance(self.km * other) + return NotImplemented + + def __truediv__(self, other: int) -> "Distance": + if isinstance(other, (int, float)): + return Distance(round(self.km / other, 2)) + return NotImplemented + + def __lt__(self, other: int) -> 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: int) -> 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: int) -> 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: int) -> bool: + return self < other or self == other + + def __ge__(self, other: int) -> bool: + return self > other or self == other From 71e4040465b61a115c4739110c034b0995b19f48 Mon Sep 17 00:00:00 2001 From: Ivan Fursyk Date: Thu, 3 Oct 2024 21:11:03 +0300 Subject: [PATCH 2/2] Solution --- app/main.py | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/app/main.py b/app/main.py index 14292803..c50ea047 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,9 @@ +from __future__ import annotations + + class Distance: - def __init__(self, km: int) -> None: + def __init__(self, km: int | float) -> None: self.km = km def __str__(self) -> str: @@ -9,53 +12,47 @@ def __str__(self) -> str: def __repr__(self) -> str: return f"Distance(km={self.km})" - def __add__(self, other: int) -> "Distance": + def __add__(self, other: int | float | Distance) -> 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: int) -> "Distance": + def __iadd__(self, other: int | float | Distance) -> Distance: if isinstance(other, Distance): self.km += other.km elif isinstance(other, (int, float)): self.km += other return self - def __mul__(self, other: int) -> "Distance": + def __mul__(self, other: int | float) -> Distance: if isinstance(other, (int, float)): return Distance(self.km * other) return NotImplemented - def __truediv__(self, other: int) -> "Distance": + def __truediv__(self, other: int | float) -> Distance: if isinstance(other, (int, float)): return Distance(round(self.km / other, 2)) return NotImplemented - def __lt__(self, other: int) -> bool: + def __lt__(self, other: int | float | Distance) -> bool: if isinstance(other, Distance): return self.km < other.km - elif isinstance(other, (int, float)): - return self.km < other - return NotImplemented + return self.km < other - def __gt__(self, other: int) -> bool: + def __gt__(self, other: int | float | Distance) -> bool: if isinstance(other, Distance): return self.km > other.km - elif isinstance(other, (int, float)): - return self.km > other - return NotImplemented + return self.km > other - def __eq__(self, other: int) -> bool: + def __eq__(self, other: int | float | Distance) -> bool: if isinstance(other, Distance): return self.km == other.km - elif isinstance(other, (int, float)): - return self.km == other - return NotImplemented + return self.km == other - def __le__(self, other: int) -> bool: + def __le__(self, other: int | float | Distance) -> bool: return self < other or self == other - def __ge__(self, other: int) -> bool: + def __ge__(self, other: int | float | Distance) -> bool: return self > other or self == other