Last active
August 29, 2015 14:04
-
-
Save MaikKlein/51cfde9d53527881b253 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define a daysBetweenDates procedure that would produce the | |
# correct output if there was a correct nextDay procedure. | |
# | |
# Note that this will NOT produce correct ouptuts yet, since | |
# our nextDay procedure assumes all months have 30 days | |
# (hence a year is 360 days, instead of 365). | |
# | |
def nextDay(year, month, day): | |
"""Simple version: assume every month has 30 days""" | |
if day < 30: | |
return year, month, day + 1 | |
else: | |
if month == 12: | |
return year + 1, 1, 1 | |
else: | |
return year, month + 1, 1 | |
def daysBetweenDates(year1, month1, day1, year2, month2, day2): | |
days = 0 | |
nd = day1 | |
nm = month1 | |
ny = year1 | |
while !(nd == day2 && nm == month2 && ny == year2): | |
(ny, nm , nd) = nextDay(ny,nm,nd) | |
days += 1 | |
return days | |
def test(): | |
test_cases = [((2012,9,30,2013,10,30),30), | |
((2012,1,1,2013,1,1),360), | |
((2012,9,1,2012,9,4),3)] | |
for (args, answer) in test_cases: | |
result = daysBetweenDates(*args) | |
if result != answer: | |
print "Test with data:", args, "failed" | |
else: | |
print "Test case passed!" | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment