From e3a07ab43fef859c608be0d8951d1bda56434d89 Mon Sep 17 00:00:00 2001 From: Olena Kalitsinska Date: Sun, 22 Oct 2023 14:51:10 +0300 Subject: [PATCH 1/2] 'Solution' --- app/main.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..5b877a0f2 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,58 @@ +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, (int, float)): + return Distance(self.km + other) + elif isinstance(other, Distance): + return Distance(self.km + other.km) + + def __iadd__(self, other: int | float | Distance) -> Distance: + if isinstance(other, (int, float)): + self.km += other + else: + self.km += other.km + return self + + def __mul__(self, other: int | float) -> Distance: + if isinstance(other, (int, float)): + return Distance(self.km * other) + + def __truediv__(self, other: int | float) -> Distance: + if isinstance(other, (int, float)): + return Distance(round(self.km / other, 2)) + + def __lt__(self, other: Distance | int | float) -> bool: + other_distance = other.km \ + if isinstance(other, Distance) else other + return self.km < other_distance + + def __gt__(self, other: Distance | int | float) -> bool: + other_distance = other.km \ + if isinstance(other, Distance) else other + return self.km > other_distance + + def __eq__(self, other: Distance | int | float) -> bool: + other_distance = other.km \ + if isinstance(other, Distance) else other + return self.km == other_distance + + def __le__(self, other: Distance | int | float) -> bool: + other_distance = other.km \ + if isinstance(other, Distance) else other + return self.km <= other_distance + + def __ge__(self, other: Distance | int | float) -> bool: + other_distance = other.km \ + if isinstance(other, Distance) else other + return self.km >= other_distance From 5d6cf8cc0e29f5f51d3b4da44ed1e683c86fcf15 Mon Sep 17 00:00:00 2001 From: Olena Kalitsinska Date: Sun, 22 Oct 2023 18:32:05 +0300 Subject: [PATCH 2/2] 'fixed' --- app/main.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/app/main.py b/app/main.py index 5b877a0f2..bfa77b767 100644 --- a/app/main.py +++ b/app/main.py @@ -33,26 +33,19 @@ def __truediv__(self, other: int | float) -> Distance: return Distance(round(self.km / other, 2)) def __lt__(self, other: Distance | int | float) -> bool: - other_distance = other.km \ - if isinstance(other, Distance) else other + other_distance = other.km if isinstance(other, Distance) else other return self.km < other_distance def __gt__(self, other: Distance | int | float) -> bool: - other_distance = other.km \ - if isinstance(other, Distance) else other + other_distance = other.km if isinstance(other, Distance) else other return self.km > other_distance def __eq__(self, other: Distance | int | float) -> bool: - other_distance = other.km \ - if isinstance(other, Distance) else other + other_distance = other.km if isinstance(other, Distance) else other return self.km == other_distance def __le__(self, other: Distance | int | float) -> bool: - other_distance = other.km \ - if isinstance(other, Distance) else other - return self.km <= other_distance + return not self > other def __ge__(self, other: Distance | int | float) -> bool: - other_distance = other.km \ - if isinstance(other, Distance) else other - return self.km >= other_distance + return not self < other