From 58fe563e86f6ffd88089bf74373d01a1a603183e Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Sun, 2 Feb 2025 01:22:06 +0100 Subject: [PATCH 1/5] solution --- app/main.py | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..d0073b211 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,44 @@ 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) -> object: + if isinstance(other, Distance): + return Distance(self.km + other.km) + if isinstance(other, (int, float)): + return Distance(self.km + other) + + def __iadd__(self, other: int) -> object: + if isinstance(other, Distance): + self.km += other.km + return self + if isinstance(other, (int, float)): + self.km += other + return self + + def __mul__(self, other: int) -> object: + return Distance(self.km * other) + + def __truediv__(self, value: int) -> object: + return Distance(round(self.km / value, 2)) + + def __lt__(self, other: int) -> bool: + return self.km < other + + def __gt__(self, other: int) -> bool: + return self.km > other + + def __eq__(self, other: int) -> bool: + return self.km == other + + def __le__(self, other: int) -> bool: + return self.km <= other + + def __ge__(self, other: int) -> bool: + return self.km >= other From f055c382c86e3b6206dde0b568a86bfbe9c3616a Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Sun, 2 Feb 2025 01:29:37 +0100 Subject: [PATCH 2/5] solution-2 --- app/main.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index d0073b211..96fa1abb7 100644 --- a/app/main.py +++ b/app/main.py @@ -29,16 +29,31 @@ def __truediv__(self, value: int) -> object: return Distance(round(self.km / value, 2)) def __lt__(self, other: int) -> bool: - return self.km < other + if isinstance(other, Distance): + return self.km < other.km + if isinstance(other, (int, float)): + return self.km < other def __gt__(self, other: int) -> bool: - return self.km > other + if isinstance(other, Distance): + return self.km > other.km + if isinstance(other, (int, float)): + return self.km > other def __eq__(self, other: int) -> bool: - return self.km == other + if isinstance(other, Distance): + return self.km == other.km + if isinstance(other, (int, float)): + return self.km == other def __le__(self, other: int) -> bool: - return self.km <= other + if isinstance(other, Distance): + return self.km <= other.km + if isinstance(other, (int, float)): + return self.km <= other def __ge__(self, other: int) -> bool: - return self.km >= other + if isinstance(other, Distance): + return self.km >= other.km + if isinstance(other, (int, float)): + return self.km >= other From 508db79b88a7da3801634964ef76f41fba566319 Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Sun, 2 Feb 2025 02:06:17 +0100 Subject: [PATCH 3/5] solution-3 --- app/main.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 96fa1abb7..0da8cfd18 100644 --- a/app/main.py +++ b/app/main.py @@ -23,10 +23,16 @@ def __iadd__(self, other: int) -> object: return self def __mul__(self, other: int) -> object: - return Distance(self.km * other) + if isinstance(other, Distance): + return Distance(self.km * other.km) + if isinstance(other, (int, float)): + return Distance(self.km * other) - def __truediv__(self, value: int) -> object: - return Distance(round(self.km / value, 2)) + def __truediv__(self, value: object) -> object: + if isinstance(value, Distance): + return Distance(round(self.km / value.km, 2)) + if isinstance(value, (int, float)): + return Distance(round(self.km / value, 2)) def __lt__(self, other: int) -> bool: if isinstance(other, Distance): From 06e1c71ece46da624698d51eeb6f4c3d1d13b272 Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Sun, 2 Feb 2025 02:11:28 +0100 Subject: [PATCH 4/5] solution-4 --- app/main.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index 0da8cfd18..79ed1a3fc 100644 --- a/app/main.py +++ b/app/main.py @@ -23,15 +23,11 @@ def __iadd__(self, other: int) -> object: return self def __mul__(self, other: int) -> object: - if isinstance(other, Distance): - return Distance(self.km * other.km) if isinstance(other, (int, float)): return Distance(self.km * other) def __truediv__(self, value: object) -> object: - if isinstance(value, Distance): - return Distance(round(self.km / value.km, 2)) - if isinstance(value, (int, float)): + if isinstance(value, (int, float)) and value != 0: return Distance(round(self.km / value, 2)) def __lt__(self, other: int) -> bool: From ff08618cccd08b686c6b214853300e5611abebda Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Sun, 2 Feb 2025 13:23:57 +0100 Subject: [PATCH 5/5] solution-5 --- app/main.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/main.py b/app/main.py index 79ed1a3fc..656646008 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,8 @@ +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: @@ -8,13 +11,13 @@ def __str__(self) -> str: def __repr__(self) -> str: return f"Distance(km={self.km})" - def __add__(self, other: int) -> object: + def __add__(self, other: Distance | int | float) -> Distance: if isinstance(other, Distance): return Distance(self.km + other.km) if isinstance(other, (int, float)): return Distance(self.km + other) - def __iadd__(self, other: int) -> object: + def __iadd__(self, other: Distance | int | float) -> Distance: if isinstance(other, Distance): self.km += other.km return self @@ -22,39 +25,39 @@ def __iadd__(self, other: int) -> object: self.km += other return self - def __mul__(self, other: int) -> object: + def __mul__(self, other: int | float) -> Distance: if isinstance(other, (int, float)): return Distance(self.km * other) - def __truediv__(self, value: object) -> object: + def __truediv__(self, value: int | float) -> Distance: if isinstance(value, (int, float)) and value != 0: return Distance(round(self.km / value, 2)) - def __lt__(self, other: int) -> bool: + def __lt__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km < other.km if isinstance(other, (int, float)): return self.km < other - def __gt__(self, other: int) -> bool: + def __gt__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km > other.km if isinstance(other, (int, float)): return self.km > other - def __eq__(self, other: int) -> bool: + def __eq__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km == other.km if isinstance(other, (int, float)): return self.km == other - def __le__(self, other: int) -> bool: + def __le__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km <= other.km if isinstance(other, (int, float)): return self.km <= other - def __ge__(self, other: int) -> bool: + def __ge__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km >= other.km if isinstance(other, (int, float)):