refactor Date.__lt__ for succinctness, same for constructor and get_earliest
This commit is contained in:
@@ -1,26 +1,18 @@
|
|||||||
|
|
||||||
|
|
||||||
class Date:
|
class Date:
|
||||||
|
|
||||||
def __init__(self, date_string):
|
def __init__(self, date_string):
|
||||||
self.month, self.day, self.year = split_date_string(date_string)
|
self.month, self.day, self.year = map(int, date_string.split('/'))
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'{self.month:02}/{self.day:02}/{self.year}'
|
return f'{self.month:02}/{self.day:02}/{self.year}'
|
||||||
|
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
for attr in ['year', 'month', 'day']:
|
for attr in ['year', 'month', 'day']:
|
||||||
if getattr(self, attr) < getattr(other, attr):
|
if getattr(self, attr) == getattr(other, attr):
|
||||||
return True
|
continue
|
||||||
elif getattr(self, attr) > getattr(other, attr):
|
return getattr(self, attr) < getattr(other, attr)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_earliest(*date_strings):
|
def get_earliest(*date_strings):
|
||||||
dates = map(Date, date_strings)
|
return str(min(map(Date, date_strings)))
|
||||||
return str(min(dates))
|
|
||||||
|
|
||||||
|
|
||||||
def split_date_string(date_string):
|
|
||||||
month, day, year = date_string.split('/')
|
|
||||||
return int(month), int(day), int(year)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user