파이썬 알고리즘 : 개인정보 수집 유효기간

자료구조

2023년 12월 04일 알고리즘 문제풀이

문제

개인정보 수집 유효기간

난이도

Lv.1

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def solution(today, terms, privacies):
    answer = []
    today_y, today_m, today_d = today.split('.')
    today_y = int(today_y)
    today_m = int(today_m)
    today_d = int(today_d)
    def check(y,m,d,t):
        ans = True
        m += t
        while m > 12:
            y += 1
            m -= 12

        if y > today_y:
            ans = False
        elif y == today_y:
            if m > today_m:
                ans = False
            elif m == today_m:
                if d > today_d:
                    ans = False
        return ans

    arr = dict()
    for x in terms:
        term_type, term_months = x.split()
        arr[term_type] = int(term_months)
    for i in range(len(privacies)):
        privacy_date, privacy_type = privacies[i].split()
        privacy_y, privacy_m, privacy_d = privacy_date.split('.')
        tmp = check(int(privacy_y),int(privacy_m),int(privacy_d),arr[privacy_type])
        if tmp:
            answer.append(i+1)
    return answer

기존에는 check() 함수 내의 while문이 존재하지 않았고, if 문을 통해 한번만 로직을 거치도록 했다. 하지만 유효기간이 2년 이상 된다면 12를 여러번 빼서 해를 몇번 넘겨야 할 수도 있기 때문에, 반복문이 필요했다. 유효기간을 더해봤자 2년 이하 일 것이라고 생각한 것이 실수였다.