Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First day or two solution in Clear category for The Most Frequent Weekdays by rossras
from calendar import isleap
from datetime import date
def most_frequent_days(year):
"""
List of most frequent days of the week in the given year
"""
# Since a year has 52*7 + (2 if leap else 1) days, we only need the first
# 1 or 2 days of the year.
result = [date(year, 1, 1).strftime('%A')]
if isleap(year):
day_two = date(year, 1, 2).strftime('%A')
result.append(day_two)
# Sort so ['Sunday', 'Monday'] becomes ['Monday', 'Sunday']
# (Everything else should be in order already)
if day_two == 'Monday':
result.reverse()
return result
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert most_frequent_days(2399) == ['Friday'], "1st example"
assert most_frequent_days(1152) == ['Tuesday', 'Wednesday'], "2nd example"
assert most_frequent_days(56) == ['Saturday', 'Sunday'], "3rd example"
assert most_frequent_days(2909) == ['Tuesday'], "4th example"
Aug. 17, 2018