# A python function that takes in three integer arguments (year, month, day) and returns an integer from 1 to 366 representing # the day within the year. For example, day_in_year(12,1,1) returns 1, day_in_year(2012,2,29) returns 60, and # day_in_year(2012,12,31) returns 366. def is_leap_year(year): # years divisible by 400 (such as 1600 or 2000) are exceptions to the usual "divisible by 4" rule if year%400 == 0: return False else: # years divisible by 4 are leap years, unless they are also divisible by 400 if year%4 == 0: return True else: return False def day_in_year(year, month, day): # Concept: # For Jan 15, the return value should simply be 15. # For Feb 15, the return value should be however many days were in Jan (the prior month), plus 15. # For March 15, the return value should be however many total cumulateive days were in Jan and Feb, (the prior months) plus 15. # Examples in the code: # For inputs of of a February 15 date (month=2), we must look up a value of 31 (the "january prior month cumulative total") and then add 15. # To get a total value of 31 for the "first" month of Jan, we need a list with 31 in the "first" position, but lists are indexed starting with zero. # Since month=2 for Feb input, we have to subtract 2 from the input to get zero, and retrieve 31 as a "total days in the prior months" # For an input of March 15 (month=3) 3-2=1, so prior_months_days[1] will produce 59, the total days in Jan and Feb that must be added to 15 # However, if it's a leap year, we need to add 1 to that total, so that's why we have a leap_year_correction. # For inputs of January (month=1), when we subract 2 from month, we get -1, and a list lookup of listname[-1] produces the last item in the list. # There's no 365 in the list because the highest month that should come in the input is 12, and 12-2=10, so prior_months_days[10] is 334, and that's # sufficient leap_year_correction = 1 if month > 2 and is_leap_year(year) else 0 prior_months_days = [31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0] return prior_months_days[month - 2] + day + leap_year_correction def test(): test_cases = [((2012, 1, 1), 1), ((2012, 1, 31), 31), ((2012, 2, 1), 32), ((2012, 2, 29), 60), ((2012, 3, 1), 61), ((2012, 3, 31), 91), ((2012, 4, 1), 92), ((2012, 4, 30), 121), ((2012, 5, 1), 122), ((2012, 5, 31), 152), ((2012, 6, 1), 153), ((2012, 6, 30), 182), ((2012, 7, 1), 183), ((2012, 7, 31), 213), ((2012, 8, 1), 214), ((2012, 8, 31), 244), ((2012, 9, 1), 245), ((2012, 9, 30), 274), ((2012, 10, 1), 275), ((2012, 10, 31), 305), ((2012, 11, 1), 306), ((2012, 11, 30), 335), ((2012, 12, 1), 336), ((2012, 12, 31), 366), ((2013, 1, 1), 1), ((2013, 1, 31), 31), ((2013, 2, 1), 32), ((2013, 2, 28), 59), ((2013, 3, 1), 60), ((2013, 3, 31), 90), ((2013, 4, 1), 91), ((2013, 4, 30), 120), ((2013, 5, 1), 121), ((2013, 5, 31), 151), ((2013, 6, 1), 152), ((2013, 6, 30), 181), ((2013, 7, 1), 182), ((2013, 7, 31), 212), ((2013, 8, 1), 213), ((2013, 8, 31), 243), ((2013, 9, 1), 244), ((2013, 9, 30), 273), ((2013, 10, 1), 274), ((2013, 10, 31), 304), ((2013, 11, 1), 305), ((2013, 11, 30), 334), ((2013, 12, 1), 335), ((2013, 12, 31), 365)] passes = 0 for (args, answer) in test_cases: result = day_in_year(*args) if result != answer: print "Failed when argument was: ", args else: passes = passes + 1 print str(passes) + " out of " + str(len(test_cases)) + " test cases passed." test() #print day_in_year(2012, 2, 1)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question