HackerRank-Python

Prepare > Python > Date and Time > Calendar Module

stem_sw 2023. 8. 21. 20:13
 

Calendar Module | HackerRank

Print the day of a given date.

www.hackerrank.com

 

문제


Task

You are given a date. Your task is to find what the day is on that date.

Input Format

A single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format.

Output Format

Output the correct day in capital letters.

 

=> 일 월 년도 형식으로 날짜를 하나 줄건데, 요일을 대문자로 출력해라.

 

 

 

 

코드


import calendar

month, day, year = map(int, input().split())


print(
    calendar.day_name[calendar.weekday(year, month, day)].upper()
)

 

 

 

 

노트


calendar.TextCalendar().formatyear()

  • 달력을 보고싶을 때 사용
  • firstweek= 으로 달력에서 한 주의 시작을 무슨 요일로 할지 정할 수 있음. 월요일:0 ~ 일요일: 7
  • formatyear() 으로 어떤 연도의 달력을 출력할 것인지 지정
print(
    calendar.TextCalendar(firstweekday=6).formatyear(year)
)
더보기

OUTPUT

                                  2015
      January                   February                   March
Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa
             1  2  3       1  2  3  4  5  6  7       1  2  3  4  5  6  7
 4  5  6  7  8  9 10       8  9 10 11 12 13 14       8  9 10 11 12 13 14
11 12 13 14 15 16 17      15 16 17 18 19 20 21      15 16 17 18 19 20 21
18 19 20 21 22 23 24      22 23 24 25 26 27 28      22 23 24 25 26 27 28
25 26 27 28 29 30 31                                29 30 31
       April                      May                       June
Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa
          1  2  3  4                      1  2          1  2  3  4  5  6
 5  6  7  8  9 10 11       3  4  5  6  7  8  9       7  8  9 10 11 12 13
12 13 14 15 16 17 18      10 11 12 13 14 15 16      14 15 16 17 18 19 20
19 20 21 22 23 24 25      17 18 19 20 21 22 23      21 22 23 24 25 26 27
26 27 28 29 30            24 25 26 27 28 29 30      28 29 30
                          31
        July                     August                  September
Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa
          1  2  3  4                         1             1  2  3  4  5
 5  6  7  8  9 10 11       2  3  4  5  6  7  8       6  7  8  9 10 11 12
12 13 14 15 16 17 18       9 10 11 12 13 14 15      13 14 15 16 17 18 19
19 20 21 22 23 24 25      16 17 18 19 20 21 22      20 21 22 23 24 25 26
26 27 28 29 30 31         23 24 25 26 27 28 29      27 28 29 30
                          30 31
      October                   November                  December
Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa
             1  2  3       1  2  3  4  5  6  7             1  2  3  4  5
 4  5  6  7  8  9 10       8  9 10 11 12 13 14       6  7  8  9 10 11 12
11 12 13 14 15 16 17      15 16 17 18 19 20 21      13 14 15 16 17 18 19
18 19 20 21 22 23 24      22 23 24 25 26 27 28      20 21 22 23 24 25 26
25 26 27 28 29 30 31      29 30                     27 28 29 30 31

 

 

calendar.weekday(year, month, day)

해당 날짜의 요일을 숫자로 리턴

print(
    calendar.weekday(year, month, day)
)
# 2
# 월요일이 0, 2는 수요일 이라는 뜻

 

 

calendar.day_name

요일을 담은 객체? 로 생각

print(
	list(calendar.day_name)
)
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  • 리스트에 담아서 출력한 모습

 

 

 

 

참조


 

 

calendar — General calendar-related functions

Source code: Lib/calendar.py This module allows you to output calendars like the Unix cal program, and provides additional useful functions related to the calendar. By default, these calendars have...

docs.python.org

 

 

Python calendar : day_name (요일, 요일 list, 요일 목록, weekday)

calendar library의 day_name은 요일 정보를 return해줍니다. import calendar as c print(c.day_name) print(list(c.day_name)) -- Result ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] day_name 자체는 요일을 담은

cosmosproject.tistory.com

https://github.com/sword-ninja/HackerRank-Solution/blob/master/Python/Date%20and%20Time/1-calendar-module.py