Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
from __future__ import annotations


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 | float) -> Distance:
return Distance(self.km
+ (other.km if isinstance(other, Distance) else other))

def __iadd__(self, other: Distance | float) -> Distance:
if isinstance(other, Distance):
self.km += other.km
else:
self.km += other
Comment on lines +19 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This else statement is unnecessary according to checklist item #1. After the if block modifies self.km, the else block performs the same action on different data. You can restructure this to avoid the else.

return self

def __mul__(self, other: float) -> Distance:
return Distance(self.km * other)

def __truediv__(self, other: float) -> Distance:
return Distance(round(self.km / other, 2))

def __lt__(self, other: Distance | float) -> bool:
return self.km < (other.km if isinstance(other, Distance) else other)

def __gt__(self, other: Distance | float) -> bool:
return self.km > (other.km if isinstance(other, Distance) else other)

def __le__(self, other: Distance | float) -> bool:
return self.km <= (other.km if isinstance(other, Distance) else other)

def __ge__(self, other: Distance | float) -> bool:
return self.km >= (other.km if isinstance(other, Distance) else other)

def __eq__(self, other: Distance | float) -> bool:
return self.km == (other.km if isinstance(other, Distance) else other)
Loading