From a9626336f554c1a937bd7d10af1b362e7b79c43a Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Wed, 1 Jul 2026 22:39:18 +0200 Subject: [PATCH 1/7] Solution --- app/main.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..fd358147e 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,65 @@ class Distance: - # Write your code here - pass + + def __init__(self, km : float) -> 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(km=self.km + other.km) + elif isinstance(other, int): + return Distance(km=self.km + other) + + def __iadd__(self, other : Distance | int) -> Distance: + if isinstance(other, Distance): + self.km += other.km + return self + elif isinstance(other, int): + self.km += other + return self + return NotImplemented + + def __mul__(self, other : int) -> Distance: + if isinstance(other, int): + self.km *= other + return self + + def __truediv__(self, other : int) -> Distance: + if isinstance(other, int): + self.km /= other + return self + + def __lt__(self, other : Distance | int) -> Distance: + if isinstance(other, Distance): + return self.km < other.km + elif isinstance(other, int): + return self.km < other + + def __gt__(self, other : Distance | int) -> Distance: + if isinstance(other, Distance): + return self.km > other.km + elif isinstance(other, int): + return self.km > other + + def __eq__(self, other: Distance | int) -> Distance: + if isinstance(other, Distance): + return self.km == other.km + elif isinstance(other, int): + return self.km == other + + def __le__(self, other: Distance | int) -> Distance: + if isinstance(other, Distance): + return self.km <= other.km + elif isinstance(other, int): + return self.km <= other + + def __ge__(self, other: Distance | int) -> Distance: + if isinstance(other, Distance): + return self.km >= other.km + elif isinstance(other, int): + return self.km >= other From 1bb0d3e81f0d6b63705e9d1546214e495ee1f66b Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Wed, 1 Jul 2026 22:55:33 +0200 Subject: [PATCH 2/7] changes mistakes --- app/main.py | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/app/main.py b/app/main.py index fd358147e..8f4a4feb9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,65 +1,74 @@ +from __future__ import annotations + + class Distance: def __init__(self, km : float) -> None: self.km = km def __str__(self) -> str: - return f"Distance : {self.km} kilometers." + return f"Distance: {self.km} kilometers." def __repr__(self) -> str: - return f"Distance(km={self.km}" + return f"Distance(km={self.km})" def __add__(self, other : Distance | int) -> Distance: if isinstance(other, Distance): return Distance(km=self.km + other.km) - elif isinstance(other, int): + if isinstance(other, int): return Distance(km=self.km + other) def __iadd__(self, other : Distance | int) -> Distance: if isinstance(other, Distance): self.km += other.km return self - elif isinstance(other, int): + if isinstance(other, int): self.km += other return self return NotImplemented - def __mul__(self, other : int) -> Distance: + def __mul__(self, other : int | float) -> Distance: if isinstance(other, int): - self.km *= other - return self + return Distance(km=self.km * other) + return NotImplemented - def __truediv__(self, other : int) -> Distance: + def __truediv__(self, other : int | float) -> Distance: if isinstance(other, int): - self.km /= other - return self + new_dist = round((self.km / other), 2) + return Distance(km=new_dist) + return NotImplemented - def __lt__(self, other : Distance | int) -> Distance: + def __lt__(self, other : Distance | int) -> bool: if isinstance(other, Distance): return self.km < other.km - elif isinstance(other, int): + if isinstance(other, int): return self.km < other + return NotImplemented - def __gt__(self, other : Distance | int) -> Distance: + def __gt__(self, other : Distance | int) -> bool: if isinstance(other, Distance): return self.km > other.km - elif isinstance(other, int): + if isinstance(other, int): return self.km > other + return NotImplemented - def __eq__(self, other: Distance | int) -> Distance: + def __eq__(self, other: Distance | int) -> bool: if isinstance(other, Distance): return self.km == other.km - elif isinstance(other, int): + if isinstance(other, int): return self.km == other + return NotImplemented - def __le__(self, other: Distance | int) -> Distance: + def __le__(self, other: Distance | int) -> bool: if isinstance(other, Distance): return self.km <= other.km - elif isinstance(other, int): + if isinstance(other, int): return self.km <= other + return NotImplemented - def __ge__(self, other: Distance | int) -> Distance: + def __ge__(self, other: Distance | int) -> bool: if isinstance(other, Distance): return self.km >= other.km - elif isinstance(other, int): + if isinstance(other, int): return self.km >= other + return NotImplemented From 58354cc1c117a2b91b0521109662417d921bbdc6 Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Wed, 1 Jul 2026 23:10:15 +0200 Subject: [PATCH 3/7] change problem with float --- app/main.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index 8f4a4feb9..bc6effb0d 100644 --- a/app/main.py +++ b/app/main.py @@ -12,30 +12,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(km=self.km + other.km) if isinstance(other, int): return Distance(km=self.km + other) + return NotImplemented - def __iadd__(self, other : Distance | int) -> Distance: + def __iadd__(self, other : Distance | int | float) -> Distance: if isinstance(other, Distance): self.km += other.km return self - if isinstance(other, int): + if isinstance(other, (int, float)): self.km += other return self return NotImplemented def __mul__(self, other : int | float) -> Distance: - if isinstance(other, int): - return Distance(km=self.km * other) + if isinstance(other, (int, float)): + return Distance(km=round(self.km * other, 2)) return NotImplemented def __truediv__(self, other : int | float) -> Distance: - if isinstance(other, int): - new_dist = round((self.km / other), 2) - return Distance(km=new_dist) + if isinstance(other, (int, float)): + return Distance(km=round((self.km / other), 2)) return NotImplemented def __lt__(self, other : Distance | int) -> bool: From 35bfa85514a04b61468bdddb5f5c5db9636323c9 Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Wed, 1 Jul 2026 23:20:02 +0200 Subject: [PATCH 4/7] new try --- app/main.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index bc6effb0d..31d240590 100644 --- a/app/main.py +++ b/app/main.py @@ -15,8 +15,8 @@ def __repr__(self) -> str: def __add__(self, other : Distance | int | float) -> Distance: if isinstance(other, Distance): return Distance(km=self.km + other.km) - if isinstance(other, int): - return Distance(km=self.km + other) + if isinstance(other, (int, float)): + return Distance(km=round(self.km + other, 2)) return NotImplemented def __iadd__(self, other : Distance | int | float) -> Distance: @@ -35,40 +35,40 @@ def __mul__(self, other : int | float) -> Distance: def __truediv__(self, other : int | float) -> Distance: if isinstance(other, (int, float)): - return Distance(km=round((self.km / other), 2)) + return Distance(km=round((self.km / other), 1)) return NotImplemented def __lt__(self, other : Distance | int) -> bool: if isinstance(other, Distance): return self.km < other.km - if isinstance(other, int): + if isinstance(other, (int, float)): return self.km < other return NotImplemented def __gt__(self, other : Distance | int) -> bool: if isinstance(other, Distance): return self.km > other.km - if isinstance(other, int): + if isinstance(other, (int, float)): return self.km > other return NotImplemented def __eq__(self, other: Distance | int) -> bool: if isinstance(other, Distance): return self.km == other.km - if isinstance(other, int): + if isinstance(other, (int, float)): return self.km == other return NotImplemented def __le__(self, other: Distance | int) -> bool: if isinstance(other, Distance): return self.km <= other.km - if isinstance(other, int): + if isinstance(other, (int, float)): return self.km <= other return NotImplemented def __ge__(self, other: Distance | int) -> bool: if isinstance(other, Distance): return self.km >= other.km - if isinstance(other, int): + if isinstance(other, (int, float)): return self.km >= other return NotImplemented From 6b6840102a4dbca0bfdf9a43cb08d560f138701e Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Wed, 1 Jul 2026 23:23:30 +0200 Subject: [PATCH 5/7] change round to 2 in div --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 31d240590..168529129 100644 --- a/app/main.py +++ b/app/main.py @@ -35,7 +35,7 @@ def __mul__(self, other : int | float) -> Distance: def __truediv__(self, other : int | float) -> Distance: if isinstance(other, (int, float)): - return Distance(km=round((self.km / other), 1)) + return Distance(km=round((self.km / other), 2)) return NotImplemented def __lt__(self, other : Distance | int) -> bool: From 684500ff851df834ea64caad56b0d736071c69e4 Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Thu, 2 Jul 2026 21:32:21 +0200 Subject: [PATCH 6/7] pytest passed --- app/main.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 168529129..4077548f8 100644 --- a/app/main.py +++ b/app/main.py @@ -30,7 +30,7 @@ def __iadd__(self, other : Distance | int | float) -> Distance: def __mul__(self, other : int | float) -> Distance: if isinstance(other, (int, float)): - return Distance(km=round(self.km * other, 2)) + return Distance(km=self.km * other) return NotImplemented def __truediv__(self, other : int | float) -> Distance: @@ -38,35 +38,35 @@ def __truediv__(self, other : int | float) -> Distance: return Distance(km=round((self.km / other), 2)) return NotImplemented - def __lt__(self, other : Distance | 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 return NotImplemented - def __gt__(self, other : Distance | 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 return NotImplemented - def __eq__(self, other: Distance | 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 return NotImplemented - def __le__(self, other: Distance | 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 return NotImplemented - def __ge__(self, other: Distance | int) -> bool: + def __ge__(self, other: Distance | int | float) -> bool: if isinstance(other, Distance): return self.km >= other.km if isinstance(other, (int, float)): From 9335e3a0bc16c6d0c3750138a46960bf883eb08c Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Fri, 3 Jul 2026 19:56:55 +0200 Subject: [PATCH 7/7] separeted method --- app/main.py | 87 +++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/app/main.py b/app/main.py index 4077548f8..c8bd0137f 100644 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,10 @@ from __future__ import annotations +from types import NotImplementedType class Distance: - def __init__(self, km : float) -> None: + def __init__(self, km: float) -> None: self.km = km def __str__(self) -> str: @@ -12,63 +13,63 @@ def __str__(self) -> str: def __repr__(self) -> str: return f"Distance(km={self.km})" - def __add__(self, other : Distance | int | float) -> Distance: + def _get_km(self, other: Distance | int | float + ) -> int | float | NotImplementedType: if isinstance(other, Distance): - return Distance(km=self.km + other.km) + return other.km if isinstance(other, (int, float)): - return Distance(km=round(self.km + other, 2)) + return other return NotImplemented - def __iadd__(self, other : Distance | int | float) -> Distance: - if isinstance(other, Distance): - self.km += other.km - return self - if isinstance(other, (int, float)): - self.km += other - return self - return NotImplemented + def __add__(self, other: Distance | int | float) -> Distance: + km = self._get_km(other) + if km is NotImplemented: + return NotImplemented + return Distance(round(self.km + km, 2)) - def __mul__(self, other : int | float) -> Distance: - if isinstance(other, (int, float)): - return Distance(km=self.km * other) - return NotImplemented + def __iadd__(self, other: Distance | int | float) -> Distance: + km = self._get_km(other) + if km is NotImplemented: + return NotImplemented + self.km += km + return self - def __truediv__(self, other : int | float) -> Distance: + def __mul__(self, other: int | float) -> Distance: if isinstance(other, (int, float)): - return Distance(km=round((self.km / other), 2)) + return Distance(self.km * other) return NotImplemented - def __lt__(self, other : Distance | int | float) -> bool: - if isinstance(other, Distance): - return self.km < other.km + def __truediv__(self, other: int | float) -> Distance: if isinstance(other, (int, float)): - return self.km < other + return Distance(round(self.km / other, 2)) return NotImplemented - 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 - return NotImplemented + def __lt__(self, other: Distance | int | float) -> bool: + km = self._get_km(other) + if km is NotImplemented: + return NotImplemented + return self.km < km + + def __gt__(self, other: Distance | int | float) -> bool: + km = self._get_km(other) + if km is NotImplemented: + return NotImplemented + return self.km > km 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 - return NotImplemented + km = self._get_km(other) + if km is NotImplemented: + return NotImplemented + return self.km == km 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 - return NotImplemented + km = self._get_km(other) + if km is NotImplemented: + return NotImplemented + return self.km <= km def __ge__(self, other: Distance | int | float) -> bool: - if isinstance(other, Distance): - return self.km >= other.km - if isinstance(other, (int, float)): - return self.km >= other - return NotImplemented + km = self._get_km(other) + if km is NotImplemented: + return NotImplemented + return self.km >= km