python-checkio/OReilly/days-diff.py

22 lines
909 B
Python

from datetime import datetime
def days_diff(a, b):
[a_year, a_month, a_days] = list(map(str, a))
[b_year, b_month, b_days] = list(map(str, b))
d1 = datetime.strptime("{}-{}-{}".format(a_year.rjust(4, "0"), a_month.rjust(2, "0"), a_days.rjust(2, "0")), "%Y-%m-%d")
d2 = datetime.strptime("{}-{}-{}".format(b_year.rjust(4, "0"), b_month.rjust(2, "0"), b_days.rjust(2, "0")), "%Y-%m-%d")
return abs((d2 - d1).days)
if __name__ == '__main__':
print("Example:")
print(days_diff((1982, 4, 19), (1982, 4, 22)))
# These "asserts" are used for self-checking and not for an auto-testing
assert days_diff((1982, 4, 19), (1982, 4, 22)) == 3
assert days_diff((2014, 1, 1), (2014, 8, 27)) == 238
assert days_diff((2014, 8, 27), (2014, 1, 1)) == 238
assert days_diff([1, 1, 1], [9999, 12, 31]) == 3652058
print("Coding complete? Click 'Check' to earn cool rewards!")