DEV Community

loading...

Discussion on: Daily Challenge #303 - Frequent Days in a Year

Collapse
agtoever profile image
agtoever

Now that challenge created a nice set of date related one-liners in Python.

from datetime import timedelta, date
from collections import Counter

days_in_year = lambda year: int((date(year, 12, 31) - date(year, 1, 1)).days)
iterate_year = lambda year: [date(year, 1, 1) + days * timedelta(days=1) for days in range(days_in_year(year) + 1)]
weekdays     = lambda year: [day.strftime('%A') for day in iterate_year(year)]
max_dict_val = lambda d: [key for key, value in d.items() if value == max(d.values())]

for year in [2427, 2185, 2860, 1770, 1785, 1984, 2000]:
    print(f'Most frequent day(s) in {year} is/are: {max_dict_val(Counter(weekdays(year)))}')
Enter fullscreen mode Exit fullscreen mode

Try it online!