알고리즘 스터디

[백준/파이썬][Bronze II] Have you had your birthday yet? - 9948

난쟁이 개발자 2025. 2. 6. 21:20
반응형

[Bronze II] Have you had your birthday yet? - 9948

문제 링크

성능 요약

메모리: 108384 KB, 시간: 96 ms

분류

구현, 문자열

제출 일자

2025년 2월 6일 21:17:34

문제 설명

Today it is 4th August. If you were born before 4th August (in whatever year you were born) then you have already had your 2007 birthday. If you were born after 4th August, you have not yet had your 2007 birthday. If you were born on 4th August, happy birthday! If you were born on 29th February unlucky, you do not have a birthday this year, as 2007 is not a leap year!

입력

Input will consist of a number of lines containing dates. All dates will be valid and no date will be repeated, so there will be no more than 365 lines. The format for a line will be one or two digits, a space, and the full name of a month. The name will begin with an upper case letter, and the other letters will be lower case. Input will be terminated by a line containing just 0 #.

출력

Output will consist of the word "Yes" if a person born on that date would have had their 2007 birthday by now, "No" if they would not, "Happy birthday" if the date is 4th August and "Unlucky" if the date is 29th February. The first letter on each output line is upper case, the rest are lower case.

풀이

birthdays = []
while True :
    cmd = input()
    if cmd == '0 #' :
        break
    birthdays.append(cmd)

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
DAY = '4'
MONTH = 'August'
for birthday in birthdays :
    day, month = birthday.split()

    # 2007년은 윤년이 아니므로 패스
    if month == 'February' and day == '29' :
        print('Unlucky')

    elif day == DAY and month == MONTH :
        print('Happy birthday')

    elif months.index(MONTH) > months.index(month) or (months.index(MONTH) == months.index(month) and int(day) < int(DAY)) :
        print("Yes")

    else :
        print('No')

처음에 바로 정답을 맞추지 못하였다. 

if문 전체 중 2번째 elif 문. 

현재 월이 8월 이전이면 일자를 계산하면 안 됐는데 그 부분을 빼먹어서 오답을 한 번 내었다. 이 부분만 유의하면 별 어려움 없는 문제였다고 생각한다.

반응형