본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 87

반응형

Q>

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).

(당신은 일년 정수로 주어진다 (예를 들면 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']). Week starts with Monday.

(결과는 요일 순서로 정렬 된 요일 목록이어야합니다 (예 : [월요일, 화요일]). 월요일은 월요일부터 시작됩니다.)

 

Input: Year as an int.

         (int로 표시되는 연도입니다.)

 

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

           (주중 일 (월요일부터 일요일까지) 순으로 정렬 된 가장 빈번한 요일 목록.)

 

Example:

most_frequent_days(1084) == ['Tuesday', 'Wednesday']
most_frequent_days(1167) == ['Sunday']

 

Preconditions: Year is between 1 and 9999. Week starts with Monday.

                    (1 년에서 9999 년 사이의 주. 월요일은 월요일부터 시작합니다.)

 

A>

# datetime : 날짜시간 관련 모듈
import datetime

# 숫자에 요일 지정(선행 조건문)
def getWeek(day):
    if day == 1:
        return 'Monday'
    elif day == 2:
        return 'Tuesday'
    elif day == 3:
        return 'Wednesday'
    elif day == 4:
        return 'Thursday'
    elif day == 5:
        return 'Friday'
    elif day == 6:
        return 'Saturday'
    else:
        return 'Sunday'

def most_frequent_days(year):
    days = []
    first = datetime.date(year, 1, 1)
    end = datetime.datetime(year, 12, 31)
    # isoweekday() :
    # date.weekday()와 동일하나 월요일=1 ~ 일요일=7로 나타냄.
    # (€: date(2011, 12, 24).isoweekday() == 6 )
    f = first.isoweekday()
    e = end.isoweekday()
    if f == e:
        days.append(getWeek(f))
    else:
        if f > e:
            tmp = f
            f = e
            e = tmp
        days.append(getWeek(f))
        days.append(getWeek(e))

    return days

if __name__ == '__main__':
    print("Example:")
    print(most_frequent_days(1084))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert most_frequent_days(1084) == ['Tuesday', 'Wednesday']
    assert most_frequent_days(1167) == ['Sunday']
    assert most_frequent_days(1216) == ['Friday', 'Saturday']
    assert most_frequent_days(1492) == ['Friday', 'Saturday']
    assert most_frequent_days(1770) == ['Monday']
    assert most_frequent_days(1785) == ['Saturday']
    assert most_frequent_days(212) == ['Wednesday', 'Thursday']
    assert most_frequent_days(1) == ['Monday']
    assert most_frequent_days(2135) == ['Saturday']
    assert most_frequent_days(3043) == ['Sunday']
    assert most_frequent_days(2001) == ['Monday']
    assert most_frequent_days(3150) == ['Sunday']
    assert most_frequent_days(3230) == ['Tuesday']
    assert most_frequent_days(328) == ['Monday', 'Sunday']
    assert most_frequent_days(2016) == ['Friday', 'Saturday']
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

O>

Example:
['Tuesday', 'Wednesday']
Coding complete? Click 'Check' to earn cool rewards!

Process finished with exit code 0

 

S>

https://py.checkio.org

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 89  (0) 2019.06.21
Python Learn the basics Quiz 88  (0) 2019.06.21
Python Learn the basics Quiz 87  (0) 2019.06.21
Python Learn the basics Quiz 86  (0) 2019.06.21
Python Learn the basics Quiz 85  (0) 2019.06.21
Python Learn the basics Quiz 84  (0) 2019.06.21