From 344488ffc3831ecfe1b6d9e66b3a3f1fa7bddbd5 Mon Sep 17 00:00:00 2001 From: Grigoriy Gesiod Date: Tue, 24 Oct 2023 17:26:27 +0300 Subject: [PATCH 1/2] start --- app/main.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7defa3411..fee6597b9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,8 @@ class Distance: - # Write your code here - pass + def __int__(self, km: int) -> None: + self.km = km + + def __str__(self) -> str: + return f"Distance: {self.km} kilometers." + + From 007dc419407edec84a9f525bb1c731bd2cb8da2d Mon Sep 17 00:00:00 2001 From: Grigoriy Gesiod Date: Tue, 24 Oct 2023 21:33:46 +0300 Subject: [PATCH 2/2] pytest and flake8 passed --- app/main.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fee6597b9..c88a2d6be 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,56 @@ +from __future__ import annotations + + class Distance: - def __int__(self, km: int) -> None: + def __init__(self, km: int | 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 | float) -> Distance: + if isinstance(other, Distance): + return Distance(self.km + other.km) + return Distance(self.km + other) + + def __iadd__(self, other: Distance | int | float) -> Distance: + if isinstance(other, Distance): + self.km += other.km + return self + self.km += other + return self + + def __mul__(self, other: int | float) -> Distance: + return Distance(self.km * other) + + def __truediv__(self, other: int | float) -> Distance: + + return Distance(round(self.km / other, 2)) + + def __lt__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km < other.km + return self.km < other + + def __gt__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km > other.km + return self.km > other + + def __eq__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km == other.km + return self.km == other + + def __le__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km <= other.km + return self.km <= other + def __ge__(self, other: Distance | int | float) -> bool: + if isinstance(other, Distance): + return self.km >= other.km + return self.km >= other