문제 바로가기
로직을 어떻게 짤까
- 각 달에 유효기간을 더한 날짜가 오늘 날짜보다 작으면 파기한다.(정답 배열에 삽입)
- 더한 달이 12가 넘어가는 경우를 고려해야 한다.
접근
- 오늘 날짜 비교를 위해 년/월/일을 정수로 슬라이스를 해준다.
ex) 2022 1 1
- 유효기간과 약관정보 딕셔너리를 만든다.(유효기간 또한 정수로 변환)
ex) { A : 6 }, { B : 12 }
- 각 개인정보 수집 일자를 반복문을 돌려서 비교해 준다.
1) 개인정보의 날짜와 약관정보를 분리한다.
2) 날짜를 년/월/일로 슬라이스 한다.
3) 개인정보 월에 해당 유효기간을 더한다.
4) 12가 넘어가는 경우 12보다 작아질 때까지 12를 빼주고 년에 1씩 더해준다.
ex) 유효기간이 24, 월이 10, 더하면 34기 때문에 2번 빼줘야 한다. -> while문 사용
5) 개인정보 < 오늘 날짜인 경우에만 배열에 삽입
코드
def solution(today, terms, privacies):
answer = []
time_dict = dict()
year, month, day = int(today[:4]), int(today[5:7]), int(today[8:])
for term in terms:
alpha = term[0]
time_dict[alpha] = int(term[2:])
for i in range(len(privacies)):
date, alpha = privacies[i].split()
pyear, pmonth, pday = int(privacies[i][:4]), int(privacies[i][5:7]), int(privacies[i][8:10])
pmonth += time_dict[alpha]
while pmonth > 12:
pmonth -= 12
pyear += 1
if pyear > year:
continue
elif pyear == year:
if pmonth > month:
continue
elif pmonth == month:
if pday > day:
continue
answer.append(i + 1)
return answer
```