Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
time.strptime solution in Clear category for The Most Frequent Weekdays by nickgryg
from time import strptime
def most_frequent_days(year):
week_days = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}
days_name = {0: 'Monday', 1: 'Tuesday', 2: 'Wednesday', 3: 'Thursday', 4: 'Friday', 5: 'Saturday', 6: 'Sunday'}
last_day = strptime('{:04d}-12-31'.format(year), '%Y-%m-%d')
for day in range(1, last_day.tm_yday + 1):
week_days[strptime('{:04d}-{}'.format(year, day), '%Y-%j').tm_wday] += 1
max_week_days = [key for key in week_days if week_days[key] == max(week_days.values())]
return [days_name[day] for day in max_week_days]
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"
Dec. 4, 2018