프로그래머스 LV1 - 개인정보 수집 유효기간
내 풀이
def solution(today, terms, privacies):
YYYY, MM, DD = map(int, today.split("."))
case_dict = {}
answer = []
for term in terms:
term_list = term.split()
case_dict[term_list[0]] = int(term_list[1])
for i, privacy in enumerate(privacies):
privacy_list = privacy.split()
date = privacy_list[0]
yyyy, mm , dd = map(int, date.split('.'))
case = privacy_list[1]
valid_month = case_dict[case]
mm = mm + valid_month
dd = dd - 1
if mm > 12:
while mm > 12:
yyyy += 1
mm -= 12
if dd == 0:
mm -= 1
if mm == 0:
mm = 12
dd = 28
if YYYY > yyyy:
answer.append(i+1)
elif YYYY == yyyy and MM > mm:
answer.append(i+1)
elif YYYY == yyyy and MM == mm and DD > dd:
answer.append(i+1)
return answer
다른 풀이
def date_comparison(expiration_date, today):
#유효기간 남았으면 True 리턴
#유효기간 끝났으면 False 리턴
if expiration_date[0] > today[0]: #year 비교
return True
if expiration_date[0] == today[0] and expiration_date[1] > today[1]: #month 비교
return True
if expiration_date[0] == today[0] and expiration_date[1] == today[1] and expiration_date[2] > today[2]: #day 비교
return True
return False
def solution(today, terms, privacies):
result = []
i = 1
today = list(map(int,today.split(".")))
#2022.05.19문자열을 "."으로 나눈 후 list안에 int형으로 바꿔 넣기
#[2022, 5, 19]
expiration = {term[0]:int(term[2:]) for term in terms}
#유효기간 dict 예) {"A" : 3, "B" : 12, "C" : 3}
for pri in privacies:
pri = pri.split(" ")
#['2021.05.02', 'A']
pri_date = list(map(int,pri[0].split(".")))
#[2021, 5, 2]
pri_date[1] += expiration[pri[1]]
#pri_date의 month에 유효기간 더하기
#[2021, 5+a, 2]
if (pri_date[1] > 12):
#month에 유효기간 더했을 때 12 넘을시
if (pri_date[1] % 12 == 0):# month가 12의 배수인경우
pri_date[0] += (pri_date[1] // 12) - 1 #year에 month를 12나눈 몫-1 만큼 더하기
pri_date[1] = 12 #month는 12고정
else:
pri_date[0] += pri_date[1] // 12 #year에 month로 12나눈 몫 만큼 더하기
pri_date[1] %= 12 #month는 12로 나눈 나머지 넣기
if date_comparison(pri_date, today) == False : #유효기간 지났을 시
result.append(i) # result에 i 추가
i += 1
return result
풀이 해석
date_comparison 함수를 만들어 이용하였다. 유효기간을 더했을 때 12가 넘으면 해결하는 방식을 몫과 나머지를 이용하여 표현하였다.
배울 점
- 새로운 함수를 만들어서 이용
- expiration = {term[0]:int(term[2:]) for term in terms}
- 위와 같이 한줄로 dict를 만드는 스킬
- 몫과 나머지의 방법으로 mont 12넘었을 때 해결하기
댓글남기기