코테연습문제

[Programmers/Python] 개인정보 수집 유효기간

SI-AH 2023. 4. 15. 14:19

 

def solution(today, terms, privacies):
    answer = []
    termsDict = {}
    # terms를 dictionary로 바꿔 활용
    for term in terms:
        a, b = term.split()
        termsDict[a] = int(b)

    # today를 나눠놓기
    tyear, tmonth, tdate = map(int, today.split('.'))
    nyear, nmonth, ndate = 0, 0, 0

    # privacies에서 띄어쓰기 기준으로 split
    for privacy in range(len(privacies)):
        day, types = privacies[privacy].split()
        pyear, pmonth, pdate = map(int, day.split('.'))
        n = termsDict[types]
        nyear = pyear
        nmonth = pmonth + n
        ndate = pdate
        
        # 약관 종류에 따라 파기해야될 날짜
        while (nmonth > 12):
            nyear += 1
            nmonth -= 12
            
            
        if tyear > nyear:
            answer.append(privacy + 1)
        elif tyear == nyear:
            if tmonth > nmonth:
                answer.append(privacy + 1)
            elif tmonth == nmonth:
                if tdate >= ndate:
                    answer.append(privacy + 1)
    answer.sort()
    return answer

유의했던 사항

1.  모든 달은 28일까지 → 윤달, 29~31일까지 고려하지 않아도 됨

2. 유효기간은 2년 이상일 수 있음 → 유효기간을 계산할 때, 한 번만 처리해서 되는 것이 아님

 

풀이과정

1. terms를 dictionary로 바꿔서 활용할 수 있도록 함 {"A": 12}의 형태

2. today의 날짜를 연,월,일 각각으로 split ('.'을 기준으로 split)

3. privacies에서 하나씩 돌면서 띄어쓰기 기준으로 split → day / types

3-1. privacies에 있는 가입 날짜를 연,월,일 각각으로 split ('.'을 기준으로 split)

4. terms의 dictionary에서 types를 key로 갖는 value값을 privacies에 주어진 가입한 달에 더함

4-1. if 월 > 12: 연 + 1, 월 - 12

(월이 12보다 작거나 같아질 때까지 수행해 유효기간이 길어져도 연월일이 정상적으로 표현되도록 한다.)

5. today와 비교해, 오늘 날짜보다 과거라면 answer.append(i)


처음 작성했던 코드(성공) - 슬라이싱을 이용한 풀이

def solution(today, terms, privacies):
    answer = []
    splitTerms = list(map(int, today.split('.')))
    tYear, tMonth, tDay = splitTerms[0], splitTerms[1], splitTerms[2]
    nYear, nMonth, nDay = 0, 0, 0
    for j in range(len(privacies)):
        for i in range(len(terms)):
            term = terms[i].split()

            if term[0] == privacies[j][-1]:
                nYear = int(privacies[j][:4])
                nMonth = int(privacies[j][5:7]) + int(term[1])
                nDay = int(privacies[j][8:10])

                while (nMonth > 12):
                    nYear += 1
                    nMonth -= 12
            else:
                 continue   
                    
            if tYear > nYear:
                answer.append(j + 1)
            elif tYear == nYear:
                if tMonth > nMonth:
                    answer.append(j + 1)
                elif tMonth == nMonth:
                    if tDay >= nDay:
                        answer.append(j + 1)
                    

        answer.sort()
    return answer

 

 

 


 

 

-) 한 번 풀었던 문제라 풀기는 빨리 풀었는데 유의했던 사항 2번('유효기간은 2년 이상일 수 있음')을 고려하지 못해 시간이 더 걸렸다.

 

+) 처음 풀었을 때와는 다르게 terms를 dictionary로 해 풀었는데, slicing으로 풀었던 것 보다 코드가 훨씬 직관적이고, 속도가 빨라졌다. 처음 풀었을 때는 약관 내용을 처리하는 과정을 위해 2중 for문을 사용했는데, 이번에는 dictionary에서 필요한 값만 찾아 써서 그런 것 같다.