[프로그래머스 | Lv1] 개인정보 수집 유효기간 (Python) - 2023 KAKAO BLIND RECRUITMENT
https://school.programmers.co.kr/learn/courses/30/lessons/150370
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
나온지 얼마 안 된 문제!
문제는 간단한데 문자열로 푸느라 머리 깨지는 줄 알았다ㅠ 숫자로 하면 쉽다,,!
(처음 푼 방법)- 문자열로 접근
1. terms -> 딕셔너리로 저장, today -> [y, m, d] 형태로 저장
2. privacies 분리해서 date = [y, m, d] 형태로 저장
3. 딕셔너리에서 해당 약관 찾아서 날짜에 개월 수 더해줌
ㄴ 해당 개월 수 // 12 를 y에 더하고
ㄴ 해당 개월수 % 12 를 m에 더하고
ㄴ 이때, 만약 m이 12보다 크면 y+1, m-12
4. 날짜에서 하루 빼기
ㄴ d=1이면 d=28, m-1
ㄴ m=0이면 y-1, m-12
ㄴ d!=1이면 d-1
5. 날짜 비교해서 today가 더 크면 answer에 추가
ㄴyear -> month -> day 순으로 비교
# 프로그래머스 150370: 개인정보 수집 유효기간 (2023 KAKAO BLIND RECRUITMENT)
def solution(today, terms, privacies):
answer = []
todayDate = list(map(int, today.split('.')))
terms_dic = {}
for i in terms:
terms_dic[i.split()[0]] = int(i.split()[1])
n = 1
for i in privacies:
date = list(map(int, i.split()[0].split('.')))
date[0] += terms_dic[i.split()[1]] // 12
date[1] += terms_dic[i.split()[1]] % 12
if date[1] > 12:
date[0] += 1
date[1] -= 12
if date[2] == 1:
date[2] = 28
date[1] -= 1
if date[1] == 0:
date[0] -= 1
date[1] = 12
else:
date[2] -= 1
if todayDate[0] > date[0]:
answer.append(n)
elif todayDate[0] == date[0]:
if todayDate[1] > date[1]:
answer.append(n)
elif todayDate[1] == date[1]:
if todayDate[2] > date[2]:
answer.append(n)
n += 1
return answer
(다시 푼 방법) - 숫자로 접근
문자열로 비교하느라 코드가 굉장히 복잡하고 지저분해져서 날짜를 모두 숫자로 바꿔서 비교했다
1. 마찬가지로 terms -> 딕셔너리로 저장, today -> [y, m, d]로 저장
ㄴ terms에서 value는 개월수*28 (문제에서 한 달은 28일)
ㄴ today에서 (y*12*28 + m*28 + d)를 숫자로 저장 (12달, 28일)
2. 마찬가지로 privacies 분리해서 date = [y, m, d] 형태로 저장
ㄴ date에서 (y*12*28 + m*28 + d) + 딕셔너리에서 해당 약관 찾아서 날짜 더해줌 - 1(하루 빼기)
3. today와 date 비교해서 today가 더 크면 answer에 추가
# 프로그래머스 150370: 개인정보 수집 유효기간 (2023 KAKAO BLIND RECRUITMENT)
def solution(today, terms, privacies):
answer = []
terms_dic = {}
for i in terms:
terms_dic[i.split()[0]] = int(i.split()[1]) * 28
todayDate = list(map(int, today.split('.')))
td = todayDate[0] * 12 * 28 + todayDate[1] * 28 + todayDate[2]
n = 1
for i in privacies:
date = list(map(int, i.split()[0].split('.')))
d = date[0] * 12 * 28 + date[1] * 28 + date[2] + terms_dic[i.split()[1]] - 1
if td > d:
answer.append(n)
n += 1
return answer