-
-
Save ahmed-abdelazim/ba21230a0e2ead0623ad552b6e08f463 to your computer and use it in GitHub Desktop.
Solution for Udacity python problem (calculate age between two dates in days)
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
# This is The best Solution I found. forked from original code writer | |
# Udacity's Intro to Programming Nanodegree. | |
# Original problem statement: | |
# Given your birthday and the current date, calculate your age | |
# in days. Compensate for leap days. Assume that the birthday | |
# and current date are correct dates. | |
# Simply put, if you were born 1 Jan 2012 and todays date is | |
# 2 Jan 2012 you are 1 day old. | |
daysOfMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
def isLeapYear(year): | |
## | |
# Your code here. Return True or False | |
# Pseudo code for this algorithm is found at | |
# http://en.wikipedia.org/wiki/Leap_year#Algorithm | |
## | |
if not year % 4 == 0: | |
return False | |
elif not year % 100 == 0: | |
return True | |
elif not year % 400 == 0: | |
return False | |
return True | |
def daysBetweenDates(y1, m1, d1, y2, m2, d2): | |
## | |
# Your code here. | |
## | |
days = 0 | |
# scenario: same years | |
if y2 == y1: | |
# scenario: same years, same months | |
if m2 == m1: | |
days = d2 - d1 | |
# scenario: same years, different months | |
else: | |
month = m1 | |
while month <= m2: | |
days += (daysOfMonths[month - 1]) | |
month += 1 | |
# Now rationalise 'days' to deduct extra days added from | |
# month of birth as well as the current year | |
days = days - d1 - (daysOfMonths[m2 - 1] - d2) | |
if isLeapYear(y1): | |
days += 1 | |
# scenario: different years | |
else: | |
days = 0 | |
year = y1 | |
month = m1 | |
while year <= y2: | |
while month <= 12: | |
if isLeapYear(year) and month == 2: | |
days += (daysOfMonths[month - 1] + 1) | |
else: | |
days += daysOfMonths[month - 1] | |
month += 1 | |
if year == y2 and month > m2: | |
break | |
year += 1 | |
month = 1 | |
# Now rationalise 'days' to deduct extra days added from | |
# month of birth as well as the current year | |
days -= d1 | |
if isLeapYear(year) and m2 == 2: | |
days -= (daysOfMonths[m2 - 1] + 1 - d2) | |
else: | |
days -= (daysOfMonths[m2 - 1] - d2) | |
return days | |
print(daysBetweenDates(1912, 12, 12, 2012, 12, 12)) | |
# same year same month | |
print(daysBetweenDates(2017, 1, 1, 2017, 1, 15)) | |
print(daysBetweenDates(2017, 1, 1, 2017, 1, 2)) | |
print(daysBetweenDates(2012, 12, 7, 2012, 12, 7)) | |
# same year different months | |
print(daysBetweenDates(2017, 1, 1, 2017, 2, 15)) | |
# different years | |
print(daysBetweenDates(1980, 10, 15, 2017, 2, 7)) | |
print(daysBetweenDates(2012, 6, 29, 2013, 6, 29)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment