Skip to content

Commit

Permalink
Solution3
Browse files Browse the repository at this point in the history
  • Loading branch information
chiper9 committed Feb 27, 2025
1 parent 945c490 commit 2e43705
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

class Distance:
def __init__(self, km: int) -> None:
self.km = km
Expand All @@ -14,15 +16,15 @@ def __repr__(self) -> str:
# operand type(s)
# for |: 'types.UnionType' and 'str'

def __add__(self, other: int | float) -> "Distance":
def __add__(self, other: Union[int, float, 'Distance']) -> "Distance":
if isinstance(other, Distance):
return Distance(self.km + other.km)
elif isinstance(other, (int, float)):
return Distance(self.km + other)
raise (TypeError(f"Unsupported operand type(s): "
f"'Distance' and {type(other)}"))

def __iadd__(self, other: int | float) -> "Distance":
def __iadd__(self, other: Union[int, float, 'Distance']) -> "Distance":
if isinstance(other, Distance):
self.km += other.km
elif isinstance(other, (int, float)):
Expand All @@ -32,53 +34,53 @@ def __iadd__(self, other: int | float) -> "Distance":
f"'Distance' and {type(other)}")
return self

def __mul__(self, other: int | float) -> "Distance":
def __mul__(self, other: Union[int, float, 'Distance']) -> "Distance":
if isinstance(other, (int, float)):
return Distance(self.km * other)
raise TypeError(f"Unsupported operand type(s): "
f"'Distance' and {type(other)}")

def __truediv__(self, other: int | float) -> "Distance":
def __truediv__(self, other: Union[int, float, 'Distance']) -> "Distance":
if isinstance(other, (int, float)):
if other == 0:
raise ZeroDivisionError("Division by zero is not allowed")
return Distance(round(self.km / other, 2))
raise TypeError(f"Unsupported operand type(s): "
f"'Distance' and {type(other)}")

def __lt__(self, other: int | float) -> bool:
def __lt__(self, other: Union[int, float, 'Distance']) -> bool:
if isinstance(other, Distance):
return self.km < other.km
elif isinstance(other, (int, float)):
return self.km < other
raise TypeError(f"Unsupported operand type(s): "
f"'Distance' and {type(other)}")

def __gt__(self, other: int | float) -> bool:
def __gt__(self, other: Union[int, float, 'Distance']) -> bool:
if isinstance(other, Distance):
return self.km > other.km
elif isinstance(other, (int, float)):
return self.km > other
raise TypeError(f"Unsupported operand type(s): "
f"'Distance' and {type(other)}")

def __eq__(self, other: int | float) -> bool:
def __eq__(self, other: Union[int, float, 'Distance']) -> bool:
if isinstance(other, Distance):
return self.km == other.km
elif isinstance(other, (int, float)):
return self.km == other
raise TypeError(f"Unsupported operand type(s): "
f"'Distance' and {type(other)}")

def __le__(self, other: int | float) -> bool:
def __le__(self, other: Union[int, float, 'Distance']) -> bool:
if isinstance(other, Distance):
return self.km <= other.km
elif isinstance(other, (int, float)):
return self.km <= other
raise TypeError(f"Unsupported operand type(s): "
f"'Distance' and {type(other)}")

def __ge__(self, other: int | float) -> bool:
def __ge__(self, other: Union[int, float, 'Distance']) -> bool:
if isinstance(other, Distance):
return self.km >= other.km
elif isinstance(other, (int, float)):
Expand Down

0 comments on commit 2e43705

Please sign in to comment.