From ec4bc6b992d83177b5057e928224a4a210e93237 Mon Sep 17 00:00:00 2001 From: Andrey1104 Date: Sun, 22 Oct 2023 20:19:30 +0300 Subject: [PATCH 1/3] solution --- app/main.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..46984514c 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,63 @@ +from typing import Any + + 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: Any) -> Any: + if isinstance(other, Distance): + return Distance(self.km + other.km) + return Distance(self.km + other) + + def __iadd__(self, other: Any) -> Any: + if isinstance(other, Distance): + self.km += other.km + return self + self.km += other + return self + + def __mul__(self, other: Any) -> Any: + if not isinstance(other, Distance): + return Distance(self.km * other) + + def __truediv__(self, other: Any) -> Any: + if not isinstance(other, Distance): + return Distance(round(self.km / other, 2)) + + def __lt__(self, other: Any) -> bool: + if isinstance(other, Distance): + return self.km < other.km + return self.km < other + + def __gt__(self, other: Any) -> bool: + if isinstance(other, Distance): + return self.km > other.km + return self.km > other + + def __eq__(self, other: Any) -> bool: + if isinstance(other, Distance): + return self.km == other.km + return self.km == other + + def __le__(self, other: Any) -> bool: + if isinstance(other, Distance): + return self.km <= other.km + return self.km <= other + + def __ge__(self, other: Any) -> bool: + if isinstance(other, Distance): + return self.km >= other.km + return self.km >= other + + def __ne__(self, other: Any) -> bool: + if isinstance(other, Distance): + return self.km != other.km + return self.km != other From 2586a3a9e28322a0d46c0763e46e49669908894c Mon Sep 17 00:00:00 2001 From: Andrey1104 Date: Mon, 23 Oct 2023 19:39:15 +0300 Subject: [PATCH 2/3] fix ge and le methods --- app/main.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/app/main.py b/app/main.py index 46984514c..4a3710607 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,4 @@ -from typing import Any +from __future__ import annotations class Distance: @@ -12,52 +12,48 @@ def __str__(self) -> str: def __repr__(self) -> str: return f"Distance(km={self.km})" - def __add__(self, other: Any) -> Any: + def __add__(self, other: (int, float, Distance)) -> Distance: if isinstance(other, Distance): return Distance(self.km + other.km) return Distance(self.km + other) - def __iadd__(self, other: Any) -> Any: + def __iadd__(self, other: (int, float, Distance)) -> Distance: if isinstance(other, Distance): self.km += other.km return self self.km += other return self - def __mul__(self, other: Any) -> Any: + def __mul__(self, other: (int, float, Distance)) -> Distance: if not isinstance(other, Distance): return Distance(self.km * other) - def __truediv__(self, other: Any) -> Any: + def __truediv__(self, other: (int, float, Distance)) -> Distance: if not isinstance(other, Distance): return Distance(round(self.km / other, 2)) - def __lt__(self, other: Any) -> bool: + def __lt__(self, other: (int, float, Distance)) -> bool: if isinstance(other, Distance): return self.km < other.km return self.km < other - def __gt__(self, other: Any) -> bool: + def __gt__(self, other: (int, float, Distance)) -> bool: if isinstance(other, Distance): return self.km > other.km return self.km > other - def __eq__(self, other: Any) -> bool: + def __eq__(self, other: (int, float, Distance)) -> bool: if isinstance(other, Distance): return self.km == other.km return self.km == other - def __le__(self, other: Any) -> bool: - if isinstance(other, Distance): - return self.km <= other.km - return self.km <= other + def __le__(self, other: (int, float, Distance)) -> bool: + return not self.__gt__(other) - def __ge__(self, other: Any) -> bool: - if isinstance(other, Distance): - return self.km >= other.km - return self.km >= other + def __ge__(self, other: (int, float, Distance)) -> bool: + return not self.__lt__(other) - def __ne__(self, other: Any) -> bool: + def __ne__(self, other: (int, float, Distance)) -> bool: if isinstance(other, Distance): return self.km != other.km return self.km != other From c82fda84c1b376007238373c2f948ed23c92588c Mon Sep 17 00:00:00 2001 From: Andrey1104 Date: Tue, 24 Oct 2023 16:42:14 +0300 Subject: [PATCH 3/3] fix type annotation --- app/main.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/app/main.py b/app/main.py index 4a3710607..803aa6d27 100644 --- a/app/main.py +++ b/app/main.py @@ -12,48 +12,48 @@ def __str__(self) -> str: def __repr__(self) -> str: return f"Distance(km={self.km})" - def __add__(self, other: (int, float, Distance)) -> Distance: + def __add__(self, other: int | float | Distance) -> Distance: if isinstance(other, Distance): return Distance(self.km + other.km) return Distance(self.km + other) - def __iadd__(self, other: (int, float, Distance)) -> Distance: + def __iadd__(self, other: int | float | Distance) -> Distance: if isinstance(other, Distance): self.km += other.km - return self - self.km += other + else: + self.km += other return self - def __mul__(self, other: (int, float, Distance)) -> Distance: + def __mul__(self, other: int | float | Distance) -> Distance: if not isinstance(other, Distance): return Distance(self.km * other) - def __truediv__(self, other: (int, float, Distance)) -> Distance: + def __truediv__(self, other: int | float | Distance) -> Distance: if not isinstance(other, Distance): return Distance(round(self.km / other, 2)) - def __lt__(self, other: (int, float, Distance)) -> bool: + def __lt__(self, other: int | float | Distance) -> bool: if isinstance(other, Distance): return self.km < other.km return self.km < other - def __gt__(self, other: (int, float, Distance)) -> bool: + def __gt__(self, other: int | float | Distance) -> bool: if isinstance(other, Distance): return self.km > other.km return self.km > other - def __eq__(self, other: (int, float, Distance)) -> bool: + def __eq__(self, other: int | float | Distance) -> bool: if isinstance(other, Distance): return self.km == other.km return self.km == other - def __le__(self, other: (int, float, Distance)) -> bool: + def __le__(self, other: int | float | Distance) -> bool: return not self.__gt__(other) - def __ge__(self, other: (int, float, Distance)) -> bool: + def __ge__(self, other: int | float | Distance) -> bool: return not self.__lt__(other) - def __ne__(self, other: (int, float, Distance)) -> bool: + def __ne__(self, other: int | float | Distance) -> bool: if isinstance(other, Distance): return self.km != other.km return self.km != other