DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #303 - Frequent Days in a Year

What is your favorite day of the week? Check if it's the most frequent day of the week in the year.

You are given a year as an integer (e.g. 2001). You should return the most frequent day(s) of the week in that year.

Input: Year as an int.

Output: The list of most frequent days in the Gregorian calendar year.

Examples:

most_frequent_days(2427) == ['Friday']
most_frequent_days(2185) == ['Saturday']
most_frequent_days(2860) == ['Thursday', 'Friday']

Tests:

most_frequent_days(1770)
most_frequent_days(1785)
most_frequent_days(1984)
most_frequent_days(2000)

Good luck!


This challenge comes from suic on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (3)

Collapse
 
soorajsnblaze333 profile image
Sooraj (PS)

Tried a different approach

const mostFrequentDays = (year) => {
  const dayValues = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; 
  let days = [];
  days.push(dayValues[new Date('01-01-'+year).getDay()])
  if (year % 4 === 0) days.push(dayValues[new Date('01-02-'+year).getDay()]);
  return days;
}
Enter fullscreen mode Exit fullscreen mode
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
Collapse
 
agtoever profile image
agtoever • Edited

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!