Skip to content

Commit 85984ee

Browse files
committed
finds total days between given dates
1 parent 14dc01e commit 85984ee

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Define a daysBetweenDates procedure that would produce the
2+
# correct output if there was a correct nextDay procedure.
3+
#
4+
# Note that this will NOT produce correct outputs yet, since
5+
# our nextDay procedure assumes all months have 30 days
6+
# (hence a year is 360 days, instead of 365).
7+
#
8+
9+
def nextDay(year, month, day):
10+
"""Simple version: assume every month has 30 days"""
11+
if day < 30:
12+
return year, month, day + 1
13+
else:
14+
if month == 12:
15+
return year + 1, 1, 1
16+
else:
17+
return year, month + 1, 1
18+
19+
20+
def dateIsBefore(year1, month1, day1, year2, month2, day2):
21+
if year1 < year2:
22+
return True
23+
if year1 == year2:
24+
if month1 < month2:
25+
return True
26+
if month1 == month2:
27+
return day1 < day2
28+
29+
return False
30+
31+
32+
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
33+
"""Returns the number of days between year1/month1/day1
34+
and year2/month2/day2. Assumes inputs are valid dates
35+
in Gregorian calendar, and the first date is not after
36+
the second."""
37+
38+
# YOUR CODE HERE!
39+
days = 0
40+
while dateIsBefore(year1, month1, day1, year2, month2, day2):
41+
year1, month1, day1 = nextDay(year1, month1, day1)
42+
days += 1
43+
44+
return days
45+
46+
47+
def test():
48+
test_cases = [((2012, 9, 30, 2012, 10, 30), 30),
49+
((2012, 1, 1, 2013, 1, 1), 360),
50+
((2012, 9, 1, 2012, 9, 4), 3)]
51+
52+
for (args, answer) in test_cases:
53+
result = daysBetweenDates(*args)
54+
if result != answer:
55+
print("Test with data:", args, "failed")
56+
else:
57+
print("Test case passed!")
58+
59+
60+
61+
test()
62+

0 commit comments

Comments
 (0)