DEV Community

loading...

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

Collapse
peter279k profile image
peter279k

Here is the simple solution with Python, datetime.datetime and datetime.timedelta modules:

from datetime import datetime
from datetime import timedelta


def most_frequent_days(year):
    weekdays = {
        'Monday': 0,
        'Tuesday': 0,
        'Wednesday': 0,
        'Thursday': 0,
        'Friday': 0,
        'Saturday': 0,
        'Sunday': 0,
    }
    date_format = '%s-%s-%s'
    start_date = date_format % (year, '01', '01')
    end_date = date_format % ((year + 1), '01', '01')
    start = datetime.strptime(start_date, '%Y-%m-%d')

    while start.strftime('%Y-%m-%d') != end_date:
        if (start.weekday()) == 0:
            weekdays['Monday'] += 1
        elif (start.weekday()) == 1:
            weekdays['Tuesday'] += 1
        elif (start.weekday()) == 2:
            weekdays['Wednesday'] += 1
        elif (start.weekday()) == 3:
            weekdays['Thursday'] += 1
        elif (start.weekday()) == 4:
            weekdays['Friday'] += 1
        elif (start.weekday()) == 5:
            weekdays['Saturday'] += 1
        elif (start.weekday()) == 6:
            weekdays['Sunday'] += 1

        start += timedelta(days=1)

    max_value = max(list(weekdays.values()))
    res = []

    for item in list(weekdays.items()):
        week_name = item[0]
        week_day = item[1]
        if week_day == max_value:
            res.append(week_name) 

    return res

Enter fullscreen mode Exit fullscreen mode