Created
November 14, 2012 10:02
-
-
Save oldratlee/4071311 to your computer and use it in GitHub Desktop.
Get the months which First day is monday
This file contains 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
# find the months which First day is monday | |
# My related Blog: http://oldratlee.com/post/2012-11-13/1-st-day-is-monday-month-count | |
dayCountOfMonthOfYear = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
dayCountOfMonthOfLeapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
print "Day count of non-leap year: ", sum(dayCountOfMonthOfYear) | |
# 365, day count of non-leap year | |
print "Day count of leap year: ", sum(dayCountOfMonthOfLeapYear) | |
# 366, day count of Leap year | |
def getDwsMonths(dayCountOfMonth, firstDay) : | |
check = lambda m : (sum(dayCountOfMonth[0 : m]) + firstDay) % 7 == 1 | |
ret = filter(check, range(0, 12)) | |
return [x + 1 for x in ret] # adjust month index to start with 1 | |
from functools import partial | |
print "Month list for leap year: ", map(partial(getDwsMonths, dayCountOfMonthOfYear), range(0, 7)) | |
# [[5], [1, 10], [4, 7], [9, 12], [6], [2, 3, 11], [8]] | |
print "Month list for non-leap year: ", map(partial(getDwsMonths, dayCountOfMonthOfLeapYear), range(0, 7)) | |
# [[10], [1, 4, 7], [9, 12], [6], [3, 11], [2, 8], [5]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment