Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First: take advantage of multimode from statistics, available since python 3.8 solution in Clear category for The Most Frequent Weekdays by leggewie
from datetime import datetime as dt
from statistics import multimode
# To undersand the most frequent weekdays we can ignore all but the first and
# last week. All weeks in between have evenly distributed weekdays. We then
# check what weekday Jan 1 for year XY is and create a slice of weekdays from
# Jan1.weekday to Sunday and another slice from Monday to Dec31.weekday. To
# answer the quesiton we then simply count occurrences of Weekdays.
def most_frequent_days(y):
wd = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
fw = wd[dt(y,1,1).weekday():] # first week
lw = wd[:dt(y,12,31).weekday()+1] # last week
return multimode(sorted((fw+lw), key=wd.index))
June 12, 2021