
프로그래머스 Lv.1 문제이지만 2023 KAKAO BLIND RECRUITMENT에 출제된 문제라고 하여서 풀어보았다.
def solution(today, terms, privacies):
answer = []
term_dict = {}
for i in terms:
kind, period = i.split()
term_dict[kind] = int(period)
for idx, i in enumerate(privacies):
day, kind = i.split()
yy, mm, dd = map(int, day.split('.'))
term_years = term_dict[kind] // 12
term_months = term_dict[kind] % 12
## 년도에 대한 처리
yy += term_years
# 달에 대한 처리
mm += term_months
if mm > 12:
yy += 1
mm -= 12
# 일에 대한 처리
dd -= 1
if dd == 0:
mm -= 1
if mm == 0:
yy -= 1
mm = 12
dd = 28
today_yy, today_mm, today_dd = map(int, today.split('.'))
yy, mm, dd = int(yy), int(mm), int(dd)
if yy < today_yy:
answer.append(idx + 1)
if yy == today_yy and mm < today_mm:
answer.append(idx + 1)
if yy == today_yy and mm == today_mm and dd < today_dd:
answer.append(idx + 1)
return answer
레벨 1인만큼 상당히 쉬운 문제이다.
지문이 지저분하지도 않고 깔끔하면서 주어진 요구사항대로 정말 구현만 하면 되는 문제였다.
다른 사람의 풀이를 아직 확인하지는 않았지만 알고리즘 문제를 풀 때 굉장히 직관적으로 풀이를 하려는 경향이 있어서 그대로 풀어보려고 했다.
주어진 값을 구조분해할당을 하여서 풀이를 하였는데
우선 terms에서 A, B, C 같이 주어진 값에 대한 월 변화 값을 가져오기 위해 딕셔너리로 묶어주었다.
그 후 년도, 월, 일을 구조분해할당으로 풀어서 int형으로 형변환 이후 사이즈 값만 조절해주었다.
def solution(today, terms, privacies):
answer = []
term_dict = {}
for i in terms:
kind, period = i.split()
term_dict[kind] = int(period)
for idx, i in enumerate(privacies):
day, kind = i.split()
yy, mm, dd = day.split('.')
change_month = int(mm) + term_dict[kind]
# 달에 대한 처리
if change_month > 12:
yy = int(yy) + 1
mm = change_month - 12
else:
mm = change_month
# 일에 대한 처리
dd = int(dd)
if dd == 1:
mm -= 1
dd = 28
else:
dd = int(dd) - 1
today_yy, today_mm, today_dd = map(int, today.split('.'))
yy, mm, dd = int(yy), int(mm), int(dd)
if yy < today_yy:
answer.append(idx + 1)
if yy == today_yy and mm < today_mm:
answer.append(idx + 1)
if yy == today_yy and mm == today_mm and dd < today_dd:
answer.append(idx + 1)
print(yy, mm, dd)
return answer
처음에 이렇게 풀이했는데 오답이 상당히 많이 나왔다.
아마 달에 대한 처리부분인 것 같다는 생각이 들었다.
# 달에 대한 처리
if change_month > 12:
yy = int(yy) + 1
mm = change_month - 12
else:
mm = change_month
달이 24보다 크면 2년이 늘어나야한다.
여기서부터 오차 발생하였고, 1월일 때 0월로 바뀐다던지 세세한 예외처리를 해주지 않아서 오답이 많이 나왔다.
1번 문제였을 것으로 추정되는데 레벨 1인데도 시간을 상당히 걸렸다.
대략 15 ~ 20분 정도 사용한 것 같다.
예외처리를 한번 더 생각해보기에 적합한 문제 같다.