Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def is_leap_year(y):
- if y % 4 == 0:
- if y % 100 == 0:
- if y % 400 == 0:
- return True
- return False
- return True
- return False
- def days_in_month(y, m):
- daysOfMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- days = daysOfMonths[m-1]
- if m == 2 and is_leap_year(y):
- days += 1
- return days
- def next_date(y,m,d):
- d += 1
- max_d = days_in_month(y,m)
- if d > max_d:
- m += 1
- d = 1
- if m > 12:
- m = 1
- y += 1
- return (y,m,d)
- def days_between(y1,m1,d1,y2,m2,d2):
- if (y1,m1,d1) > (y2,m2,d2):
- return -days_between(y2,m2,d2,y1,m1,d1)
- count = 0
- while (y1,m1,d1) != (y2,m2,d2):
- count += 1
- (y1,m1,d1) = next_date(y1,m1,d1)
- return count
- def test():
- test_cases = [
- ((2012,1,1,2012,2,28), 58),
- ((2012,1,1,2012,3,1), 60),
- ((2011,6,30,2012,6,30), 366),
- ((2011,1,1,2012,8,8), 585 ),
- ((1900,1,1,1999,12,31), 36523)
- ]
- for ((y1,m1,d1,y2,m2,d2), expected) in test_cases:
- actual = days_between(y1,m1,d1,y2,m2,d2)
- if actual != expected:
- print("FAILED FOR",y1,m1,d1,y2,m2,d2,actual,expected)
- else:
- print("SUCCESS!")
- test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement