본문 바로가기

Algorithm/SWEA

[SWEA / python] 2056. 연월일 달력

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=1&contestProbId=AV5QLkdKAz4DFAUq&categoryId=AV5QLkdKAz4DFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=PYTHON&select-1=1&pageSize=10&pageIndex=1 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

T = int(input())

for test_case in range(1, T + 1):
    data = input()
    year = data[0:4]
    month = data[4:6]
    day = data[6:8]

    if (int(month) in [1, 3, 5, 7, 8, 10, 12]):
        if int(day) > 31:
            print("#" + str(test_case) + " -1")
        else:
            print("#" + str(test_case) + " "+ str(year)+"/"+str(month)+"/"+str(day))
    elif (int(month) in [4, 6, 9, 11]):
        if int(day) > 30:
            print("#" + str(test_case) + " -1")
        else:
             print("#" + str(test_case) + " "+ str(year)+"/"+str(month)+"/"+str(day))
    elif (int(month) == 2):
        if int(day) > 29:
            print("#" + str(test_case) + " -1")
        else:
             print("#" + str(test_case) + " "+ str(year)+"/"+str(month)+"/"+str(day))
    else:
        print("#" + str(test_case) + " -1")

딕셔너리를 쓰면 코드 길이가 줄어든다...

그리고 다시보니 이상하게 풀었는데 정답이어서 다시 고침.. 정신좀 차리고 풀자..

 

 

#수정한 코드

T = int(input())

for test_case in range(1, T + 1):
    data = input()
    year = data[0:4]
    month = data[4:6]
    day = data[6:8]

    if (int(month) in [1, 3, 5, 7, 8, 10, 12]) and 0 < int(day) < 32:
        print("#" + str(test_case) + " " + str(year) + "/" + str(month) + "/" + str(day))
    elif (int(month) in [4, 6, 9, 11]) and 0 < int(day) < 31:
            print("#" + str(test_case) + " " + str(year) + "/" + str(month) + "/" + str(day))
    elif (int(month) == 2) and 0 < int(day) < 29:
            print("#" + str(test_case) + " " + str(year) + "/" + str(month) + "/" + str(day))
    else:
        print("#" + str(test_case) + " -1")

 

#딕셔너리 사용할 경우

...

day_check= {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}

if 0< int(month) < 13 and int(day) <= day_check[int(month)]:
	print("#" + str(test_case) + " " + str(year) + "/" + str(month) + "/" + str(day))
else:
	print("#" + str(test_case) + " -1")