Skip to content

Instantly share code, notes, and snippets.

@splbio
Created April 20, 2016 19:58
Show Gist options
  • Save splbio/91a458ab784f943561318a5f087835a3 to your computer and use it in GitHub Desktop.
Save splbio/91a458ab784f943561318a5f087835a3 to your computer and use it in GitHub Desktop.
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):
"""Returns the number of days between year1/month1/day1
and year2/month2/day2. Assumes inputs are valid dates
in Gregorian calendar, and the first date is not after
the second."""
"""To do this we need to count up from year1,month1,day1 until
we get to year2,month2,day2 and track how far we counted.
"""
"""To make things clearer, lets make a copy of year1, month1,
and day1 so that we aren't re-using variables, this is just
a trick to make the code easier to read and maintain."""
year_counter = year1
month_counter = month1
day_counter= day1
# ok, now year_counter, month_counter and day_counter are the same
# as year1, month1, and day1.
"""Ok, now what we are doing to do is loop over and over using
nextDay() to add to year_counter, month_counter and day_counter,
until year_counter, month_counter and day_counter equal
year2,month2,day2, while doing that we will use the variable
'count' to count how many times we loop because that will
indicate how many days."""
count = 0 # count needs to start at zero
while [year_counter, month_counter, day_counter] != [year2, month2, day2]:
"""year_counter, month_counter, day_counter is not equal to year2, month2, day2
so we..."""
# add one to 'count'
count = count + 1
# update year_counter, month_counter, day_counter to be the next day
year_counter, month_counter, day_counter = nextDay(year_counter, month_counter, day_counter)
"""now we've reached the end of our loop, so we will jump back to the "while" above
and try again until the comparison is false."""
"""give back how many times we looped, which is how many days there are different between the
two."""
return count
def daysBetweenDates2(year1, month1, day1, year2, month2, day2):
"""Same code as before, just the while loop is written so maybe it is
easier to read."""
year_counter = year1
month_counter = month1
day_counter= day1
count = 0 # count needs to start at zero
""" What we will do is loop forever until told to stop, that's what "while True:" does.
a call to "break" will exit the loop."""
while True: # loop until we say stop
# if they are the same, then escape from this loop
if year_counter == year2 and month_counter == month2 and day_counter == day2:
break # get me out of the while loop!
# otherwise add one to 'count'
count = count + 1
# update year_counter, month_counter, day_counter to be the next day
year_counter, month_counter, day_counter = nextDay(year_counter, month_counter, day_counter)
return count
def daysBetweenDates3(year1, month1, day1, year2, month2, day2):
"""Same code as before, just the while loop is written so maybe it is
easier to read, also I changed the way we get our data back from
nextDay() so that maybe that is also easier to read."""
year_counter = year1
month_counter = month1
day_counter= day1
count = 0 # count needs to start at zero
""" What we will do is loop forever until told to stop, that's what "while True:" does.
a call to "break" will exit the loop."""
while True: # loop until we say stop
# if they are the same, then escape from this loop
if year_counter == year2 and month_counter == month2 and day_counter == day2:
break # get me out of the while loop!
# otherwise add one to 'count'
count = count + 1
"""So maybe the way we assign multiple variables coming back from nextDay() looks
weird, so instead let's just get back the "array" from nextDay and assign it
in another step so it's more clear how the data is coming back."""
# tomorrow is now an array like so [tomorrow_year, tomorrow_month, tomorrow_day]
tomorrow_array = nextDay(year_counter, month_counter, day_counter)
# now extract each element from the array into our variables
year_counter = tomorrow_array[0] # first element is year
month_counter = tomorrow_array[1] # next is month
day_counter = tomorrow_array[2] # third is day
return count
def daysBetweenDates_fast(year1, month1, day1, year2, month2, day2):
days = day2 - day1
# we subtracted and if days < 0 then we just have to "borrow" from the next
# digit (the months) just like in normal grade school arithmatic
if days < 0:
"""let's say we got -7, then we need to reset days to "23" and take away a month
how do we do that?
We add our negative days to 30 and we should get 23, right?
Then we just have to borrow a 1 from the months!"""
days = 30 + days
month2 = month2 - 1
"""ok now let's do the month, pretty much the same as days, except the range is not 30, it's 12
and we borrow from years if needed"""
months = month2 - month1
if months < 0:
months = 12 + month
year2 = year2 - 1
# ok now just subtract the years
years = year2 - year1
"""So now we have the different sorted out, but it's in years, months and days.
We need to turn it into days so let's just multiply things out."""
# first move all the years into the months:
# now we have "months + days"
months = months + (years * 12)
# now let's convert those months into days:
days = days + (months * 30)
return days
def daysBetweenDates_fast_easy(year1, month1, day1, year2, month2, day2):
""" Let's try one more super easy way...
Let's just convert both dates into "days" and then subtract them, SO EASY!!!"""
total_days1 = (year1 * 12 * 30) + (month1 * 30) + day1
total_days2 = (year2 * 12 * 30) + (month2 * 30) + day2
return total_days2 - total_days1
# Let's try a few times:
print "Number of days between 2015 and 2016 is: {}".format(daysBetweenDates(2015, 1, 1, 2016, 1, 1))
print "Number of days between Jan 1, 2015 and Jan 15, 2015 is: {}".format(
daysBetweenDates(2015, 1, 1, 2015, 1, 15))
print "Number of days between Jan 1, 1980 and Jan 15, 2015 is: {}".format(
daysBetweenDates(1980, 1, 1, 2015, 1, 15))
# using the second function
print "2nd function: Number of days between Jan 1, 1980 and Jan 15, 2015 is: {}".format(
daysBetweenDates2(1980, 1, 1, 2015, 1, 15))
# using the third function
print "3rd function: Number of days between Jan 1, 1980 and Jan 15, 2015 is: {}".format(
daysBetweenDates3(1980, 1, 1, 2015, 1, 15))
print "fast: Number of days between 2015 and 2016 is: {}".format(daysBetweenDates_fast(2015, 1, 1, 2016, 1, 1))
print "fast: Number of days between Jan 1, 2015 and Jan 15, 2015 is: {}".format(
daysBetweenDates_fast(2015, 1, 1, 2015, 1, 15))
print "fast: Number of days between Jan 1, 1980 and Jan 15, 2015 is: {}".format(
daysBetweenDates_fast(1980, 1, 1, 2015, 1, 15))
print "fast_easy: Number of days between 2015 and 2016 is: {}".format(daysBetweenDates_fast_easy(2015, 1, 1, 2016, 1, 1))
print "fast_easy: Number of days between Jan 1, 2015 and Jan 15, 2015 is: {}".format(
daysBetweenDates_fast_easy(2015, 1, 1, 2015, 1, 15))
print "fast_easy: Number of days between Jan 1, 1980 and Jan 15, 2015 is: {}".format(
daysBetweenDates_fast_easy(1980, 1, 1, 2015, 1, 15))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment