물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Most Frequent Weekdays

BEstyle 2022. 11. 27. 00:27

DESCRIPTION:

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

You are given a year as integer (e. g. 2001). You should return the most frequent day(s) of the week in that year. The result has to be a list of days sorted by the order of days in week (e. g. ['Monday', 'Tuesday'], ['Saturday', 'Sunday'], ['Monday', 'Sunday']). Week starts with Monday.

Input: Year as an int.

Output: The list of most frequent days sorted by the order of days in week (from Monday to Sunday).

Preconditions:

  • Week starts on Monday.
  • Year is between 1583 and 4000.
  • Calendar is Gregorian.

Examples (input -> output):

* 2427 -> ['Friday']
* 2185 -> ['Saturday']
* 2860 -> ['Thursday', 'Friday']

import datetime
def most_frequent_days(year):
    mon,tue,wed,thu,fri,sat,sun=0,0,0,0,0,0,0
    ans=[0,0,0,0,0,0,0]
    for i in range(1,13):
        for j in range(1,32):
            try:
                ans[datetime.datetime(year,i,j).weekday()]+=1
            except:
                pass
    ans2=[]
    for i in range(len(ans)):
        if ans[i]==max(ans):
            if i==0:
                ans2.append('Monday')
            elif i==1:
                ans2.append('Tuesday')
            elif i==2:
                ans2.append('Wednesday')
            elif i==3:
                ans2.append('Thursday')
            elif i==4:
                ans2.append('Friday')
            elif i==5:
                ans2.append('Saturday')
            elif i==6:
                ans2.append('Sunday')
    return ans2

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Christmas tree  (0) 2022.11.27
Lowest product of 4 consecutive numbers  (0) 2022.11.27
Message Validator  (0) 2022.11.27
Clocky Mc Clock-Face  (0) 2022.11.24
The Office V - Find a Chair  (0) 2022.11.24