From 833ebb9f5df0cb2efdb49e6230037882487fded1 Mon Sep 17 00:00:00 2001 From: vito Date: Sun, 26 Feb 2023 09:46:01 +0100 Subject: [PATCH 1/5] Solution --- app/main.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..0020e23e9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,66 @@ +from __future__ import annotations + + class Distance: - # Write your code here - pass + def __init__(self, km: any) -> 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: any) -> Distance: + if isinstance(other, Distance): + self.km += other.km + return self + else: + self.km += other + return self + + def __iadd__(self, other: any) -> Distance: + if isinstance(other, Distance): + self.km += other.km + return self + else: + self.km += other + return self + + def __mul__(self, other: any) -> Distance: + self.km = self.km * other + return self + + def __truediv__(self, other: any) -> Distance: + self.km = round(self.km / other, 2) + return self + + def __lt__(self, other: any) -> bool: + if isinstance(other, Distance): + return self.km < other.km + else: + return self.km < other + + def __gt__(self, other: any) -> bool: + if isinstance(other, Distance): + return self.km > other.km + else: + return self.km > other + + def __eq__(self, other: any) -> bool: + if isinstance(other, Distance): + return self.km == other.km + else: + return self.km == other + + def __le__(self, other: any) -> bool: + if isinstance(other, Distance): + return self.km <= other.km + else: + return self.km <= other + + def __ge__(self, other: any) -> bool: + if isinstance(other, Distance): + return self.km >= other.km + else: + return self.km >= other From ba401f5928618e8ea70d8d2a112deee0fb7fa265 Mon Sep 17 00:00:00 2001 From: jinjo Date: Wed, 18 Oct 2023 23:40:32 +0200 Subject: [PATCH 2/5] Solution --- app/main.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/main.py b/app/main.py index 0020e23e9..001f73458 100644 --- a/app/main.py +++ b/app/main.py @@ -11,7 +11,7 @@ def __str__(self) -> str: def __repr__(self) -> str: return f"Distance(km={self.km})" - def __add__(self, other: any) -> Distance: + def __add__(self, other: Distance | int | float) -> Distance: if isinstance(other, Distance): self.km += other.km return self @@ -19,7 +19,7 @@ def __add__(self, other: any) -> Distance: self.km += other return self - def __iadd__(self, other: any) -> Distance: + def __iadd__(self, other: Distance | int | float) -> Distance: if isinstance(other, Distance): self.km += other.km return self @@ -27,39 +27,39 @@ def __iadd__(self, other: any) -> Distance: self.km += other return self - def __mul__(self, other: any) -> Distance: + def __mul__(self, other: Distance | int | float) -> Distance: self.km = self.km * other return self - def __truediv__(self, other: any) -> Distance: + def __truediv__(self, other: Distance | int | float) -> Distance: self.km = round(self.km / other, 2) return self - def __lt__(self, other: any) -> bool: + def __lt__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km < other.km else: return self.km < other - def __gt__(self, other: any) -> bool: + def __gt__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km > other.km else: return self.km > other - def __eq__(self, other: any) -> bool: + def __eq__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km == other.km else: return self.km == other - def __le__(self, other: any) -> bool: + def __le__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km <= other.km else: return self.km <= other - def __ge__(self, other: any) -> bool: + def __ge__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km >= other.km else: From 939d7cc964f99605b3a14d6500aeeb4caef11d5b Mon Sep 17 00:00:00 2001 From: jinjo Date: Thu, 19 Oct 2023 12:33:08 +0200 Subject: [PATCH 3/5] Solution --- app/main.py | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/app/main.py b/app/main.py index 001f73458..bd390dd25 100644 --- a/app/main.py +++ b/app/main.py @@ -2,7 +2,7 @@ class Distance: - def __init__(self, km: any) -> None: + def __init__(self, km: float | int) -> None: self.km = km def __str__(self) -> str: @@ -13,54 +13,47 @@ def __repr__(self) -> str: def __add__(self, other: Distance | int | float) -> Distance: if isinstance(other, Distance): - self.km += other.km - return self - else: - self.km += other - return self + km = self.km + other.km + return Distance(km) + km = self.km + other + return Distance(km) def __iadd__(self, other: Distance | int | float) -> Distance: if isinstance(other, Distance): self.km += other.km return self - else: - self.km += other - return self + self.km += other + return self def __mul__(self, other: Distance | int | float) -> Distance: - self.km = self.km * other - return self + km = self.km * other + return Distance(km) def __truediv__(self, other: Distance | int | float) -> Distance: - self.km = round(self.km / other, 2) - return self + km = round(self.km / other, 2) + return Distance(km) def __lt__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km < other.km - else: - return self.km < other + return self.km < other def __gt__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km > other.km - else: - return self.km > other + return self.km > other def __eq__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km == other.km - else: - return self.km == other + return self.km == other def __le__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km <= other.km - else: - return self.km <= other + return self.km <= other def __ge__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km >= other.km - else: - return self.km >= other + return self.km >= other From ab11fb178633419d54fa8520553ceeb8d40e6af3 Mon Sep 17 00:00:00 2001 From: jinjo Date: Thu, 19 Oct 2023 12:52:23 +0200 Subject: [PATCH 4/5] Solution --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index bd390dd25..600d542dd 100644 --- a/app/main.py +++ b/app/main.py @@ -21,8 +21,8 @@ def __add__(self, other: Distance | int | float) -> Distance: def __iadd__(self, other: Distance | int | float) -> Distance: if isinstance(other, Distance): self.km += other.km - return self - self.km += other + else: + self.km += other return self def __mul__(self, other: Distance | int | float) -> Distance: From 8f5ba0106999a8502d6523f1e3f1ff034243982a Mon Sep 17 00:00:00 2001 From: jinjo Date: Thu, 19 Oct 2023 13:33:07 +0200 Subject: [PATCH 5/5] Solution --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 600d542dd..ad46106d3 100644 --- a/app/main.py +++ b/app/main.py @@ -14,8 +14,8 @@ def __repr__(self) -> str: def __add__(self, other: Distance | int | float) -> Distance: if isinstance(other, Distance): km = self.km + other.km - return Distance(km) - km = self.km + other + else: + km = self.km + other return Distance(km) def __iadd__(self, other: Distance | int | float) -> Distance: