Sortable Objects
Completed
Create Version("1.10.2") that compares correctly (1.10 > 1.9). Implement __lt__ and __eq__ using tuples of ints. Read a line of versions and print them sorted, then the newest.
Input
One line of versions.
Output
Two lines.
Example
Input
1.10.0 1.9.3 1.2 2.0
Output
1.2 1.9.3 1.10.0 2.0 2.0
Editor
Python
Hints
Compare tuples of ints, not strings: "10" < "9" as text.
@total_ordering fills in <=, >, >= for you.
Solution explanation
Defining __lt__ is enough for sorted, min and max.
Show solution
from functools import total_ordering
@total_ordering
class Version:
def __init__(self, text):
self.text = text
self.parts = tuple(int(p) for p in text.split("."))
def __eq__(self, other):
return self.parts == other.parts
def __lt__(self, other):
return self.parts < other.parts
def __str__(self):
return self.text
versions = sorted(Version(v) for v in input().split())
print(*versions)
print(max(versions))