1: Your program gives incorrect result, off by 1, for
Code:

print(daysBetweenDates(1871, 10, 18, # Charles Babbage
2004, 10, 19, # Ken Iverson
))

2: As you modified your functions you didn't change their names. Consequently you have functions which don't do what the names imply.

3: I had planned to time the programs. I don't know which is faster, I inserted a function call. Of course, I removed a list deletion. Anyway, I don't bother timing incorrect programs.

4: My code is 6 lines longer than yours, but then again, 29 lines of my code are explanation and tests.

Code:

'''
>>> print('Command "python -m doctest q.py" to run doctests.')
Command "python -m doctest q.py" to run doctests.
'''
def isLeapYear(year):
'''
Return True iff the year is a leap year.
>>> [isLeapYear(year) for year in (1,2,3,4,100,200,300,400,)]
[False, False, False, True, False, False, False, True]
'''
return not ((year % 4) or ((year % 100 == 0) and (year % 400)))
def LeapYears(year1, year2):
'''
return the number of leap years between the inclusive range year1 and year2
>>> LeapYears(1996, 2000)
2
'''
assert year1 <= year2
return sum(isLeapYear(year) for year in range(year1, year2+1))
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
'''
confirmed with
http://www.wolframalpha.com/input/?i=days+between+2004-OCT-19+and+1871-OCT-18
>>> daysBetweenDates(
... 1871, 10, 18, # Charles Babbage
... 2004, 10, 19, # Ken Iverson
... )
...
48579
'''
assert year1 <= year2
leap_years = LeapYears(year1, year2)
days_of_months = [31, None, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
days_of_months[1] = 28 + isLeapYear(year1)
front_subtract = sum(days_of_months[:month1 - 1]) + day1
days_of_months[1] = 28 + isLeapYear(year2)
back_subtract = sum(days_of_months[month2 - 1:]) - day2
return 365 * (1 + year2 - year1) + LeapYears(year1, year2) - front_subtract - back_subtract

Tweet This+ 1 thisPost To Linkedin