-
-
Save sumonst21/9be1cec633f012b8ae0b6c4550de76a8 to your computer and use it in GitHub Desktop.
Find months with at least 5 Fridays, Saturdays and Sundays
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
import calendar | |
import datetime | |
# change timezone to GMT+6 | |
START_YEAR = 2021 | |
END_YEAR = 2023 | |
def get_all_super_months(start_year, end_year): | |
'''find all months with at least 5 fridays, saturdays and sundays''' | |
super_months = [] | |
for year in range(start_year, end_year): | |
for month in range(1, 12): | |
this_month = {} | |
month_calendar = calendar.Calendar() | |
for day in month_calendar.itermonthdates(year, month): | |
# itermonthdates returns also days from previous and next month to complete the week | |
# http://docs.python.org/library/calendar.html | |
if day.month != month: | |
continue | |
weekday = day.weekday() | |
if weekday not in this_month: | |
this_month[weekday] = 0 | |
this_month[weekday] = this_month[weekday] + 1 | |
# now check if that was super-month | |
if this_month[4] >= 5 and this_month[5] >= 5 and this_month[6] >= 5: | |
super_months.append((year, month)) | |
return super_months | |
def print_stats(start_year, end_year, super_months): | |
'''find out how many were there, and print them''' | |
print("From year %d to year %d, I have found %d super-months\n" % (start_year, | |
end_year, len(super_months))) | |
print("List of super-months:\n") | |
for sm in super_months: | |
print("%s %s\n" % (str(sm[0]), str(sm[1]))) | |
def count_non_super_years(start_year, end_year, super_months): | |
non_super_years = [] | |
for year in range(start_year, end_year): | |
occurences_for_year = [y for (y, m) in super_months if y == year] | |
# print "occ: ", occurences_for_year | |
if len(occurences_for_year) < 1: | |
non_super_years.append(year) | |
if len(non_super_years) == 0: | |
print("All years have at least 1 super month!") | |
else: | |
print("Years without super month: ", non_super_years) | |
if __name__ == '__main__': | |
super_months = get_all_super_months(START_YEAR, END_YEAR) | |
count_non_super_years(START_YEAR, END_YEAR, super_months) | |
#print_stats(START_YEAR, END_YEAR, super_months) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment