Python @ DjangoSpin

PyPro #63 Fourth Saturdays of 2017

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Fourth Saturdays of 2017 in Python

Fourth Saturdays of 2017 in Python

Fourth Saturdays of 2017. Write a Python program to find out on which date does the fourth Saturday of each month fall on.

Fourth Saturdays of 2017

for monthNumber in range(1, 13):
    calendarOfMonthInLists = calendar.monthcalendar(2017, monthNumber)
    firstWeek  = calendarOfMonthInLists[0]			# for March 2017: [0, 0, 1, 2, 3, 4, 5] 
    fourthWeek = calendarOfMonthInLists[3]
    fifthWeek = calendarOfMonthInLists[4]

	# if the first week has a Saturday, then fourth Saturday lies in fourth week; else in fifth week.
	# to check for above condition, use the fact that value at index 5 of firstWeek is not 0
	# calendar.SATURDAY is an identifier with value 5
    if firstWeek[calendar.SATURDAY]:
        fourthSaturday = fourthWeek[calendar.SATURDAY]
    else:
        fourthSaturday = fifthWeek[calendar.SATURDAY]

    print('{}: {}'.format(calendar.month_abbr[monthNumber], fourthSaturday))
	
	
## Jan: 28
## Feb: 25
## Mar: 25
## Apr: 22
## May: 27
## Jun: 24
## Jul: 22
## Aug: 26
## Sep: 23
## Oct: 28
## Nov: 25
## Dec: 23

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply