From c22933170d2119f540eb08ebb0bc4e668ba73730 Mon Sep 17 00:00:00 2001 From: Taras Pavlovskyi Date: Wed, 22 Jan 2025 14:45:34 +0200 Subject: [PATCH 1/2] Solution --- .flake8 | 2 +- app/main.py | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/.flake8 b/.flake8 index d7459204e..99d2e123c 100644 --- a/.flake8 +++ b/.flake8 @@ -4,4 +4,4 @@ ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818 max-line-length = 79 max-complexity = 18 select = B,C,E,F,W,T4,B9,ANN,Q0,N8,VNE -exclude = venv, tests +exclude = .venv, tests diff --git a/app/main.py b/app/main.py index 7defa3411..5ac53242d 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,46 @@ +from __future__ import annotations + + class Distance: - # Write your code here - pass + def __init__(self, km: int or 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) -> Distance: + if isinstance(other, Distance): + return Distance(self.km + other.km) + else: + return Distance(self.km + other) + + def __iadd__(self, other: Distance) -> Distance: + if isinstance(other, Distance): + self.km += other.km + else: + self.km += other + return self + + def __mul__(self, other: Distance) -> Distance: + return Distance(self.km * other) + + def __truediv__(self, other: Distance) -> Distance: + return Distance(round(self.km / other, 2)) + + def __lt__(self, other: Distance) -> bool: + return self.km < other + + def __gt__(self, other: Distance) -> bool: + return self.km > other + + def __eq__(self, other: Distance) -> bool: + return self.km == other + + def __le__(self, other: Distance) -> bool: + return self.km <= other + + def __ge__(self, other: Distance) -> bool: + return self.km >= other From 19f6bb1c4c882317a7c8ac0b7fdbe0edaae4159b Mon Sep 17 00:00:00 2001 From: Taras Pavlovskyi Date: Wed, 22 Jan 2025 15:01:23 +0200 Subject: [PATCH 2/2] Solution --- app/main.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/app/main.py b/app/main.py index 5ac53242d..d64721d8a 100644 --- a/app/main.py +++ b/app/main.py @@ -2,7 +2,7 @@ class Distance: - def __init__(self, km: int or float) -> None: + def __init__(self, km: float) -> None: self.km = km def __str__(self) -> str: @@ -11,36 +11,35 @@ def __str__(self) -> str: def __repr__(self) -> str: return f"Distance(km={self.km})" - def __add__(self, other: Distance) -> Distance: + def __add__(self, other: Distance | float) -> Distance: if isinstance(other, Distance): return Distance(self.km + other.km) - else: - return Distance(self.km + other) + return Distance(self.km + other) - def __iadd__(self, other: Distance) -> Distance: + def __iadd__(self, other: Distance | float) -> Distance: if isinstance(other, Distance): self.km += other.km - else: - self.km += other + return self + self.km += other return self - def __mul__(self, other: Distance) -> Distance: + def __mul__(self, other: Distance | float) -> Distance: return Distance(self.km * other) - def __truediv__(self, other: Distance) -> Distance: + def __truediv__(self, other: Distance | float) -> Distance: return Distance(round(self.km / other, 2)) - def __lt__(self, other: Distance) -> bool: + def __lt__(self, other: Distance | float) -> bool: return self.km < other - def __gt__(self, other: Distance) -> bool: + def __gt__(self, other: Distance | float) -> bool: return self.km > other - def __eq__(self, other: Distance) -> bool: + def __eq__(self, other: Distance | float) -> bool: return self.km == other - def __le__(self, other: Distance) -> bool: + def __le__(self, other: Distance | float) -> bool: return self.km <= other - def __ge__(self, other: Distance) -> bool: + def __ge__(self, other: Distance | float) -> bool: return self.km >= other