알고리즘/Python

[python] 프로그래머스 - 개인정보 수집 유효기간

제주도랏맨 2023. 3. 9. 17:55

출처 : 프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

더보기

 

먼저, 생각해야 할 부분

 

1. 어떻게 해야 기간이 지났다는 것을 명확하게 알 수 있을까?

     년, 월, 일을 일일이 비교하는 것보다 두 날짜 사이의 일자를 구해서 하나의 정수로 비교하는게 쉽다.

 

2. 유효기간과 지난 일자가 같을 경우와 같은 엣지 케이스에 주의하자.

 

 


 

풀이

 

from collections import defaultdict

def solution(today, terms, privacies):
    answer = []
    
    t_year, t_month, t_date = map(int, today.split('.'))
    
    terms_detail = defaultdict(int)
    
    for term in terms:
        policy, due_month = map(lambda x: x if x.isalpha() else int(x), term.split())
        terms_detail[policy] = due_month * 28
        
    for i in range(len(privacies)):
        privacy = privacies[i]
        start_day, term = privacy.split()
        year, month, date = map(int, start_day.split('.'))
        
        diff_year = t_year - year
        diff_month = t_month - month
        diff_date = t_date - date
        
        total_diff = diff_year * 28 * 12 + diff_month * 28 + diff_date
        
        if total_diff >= terms_detail[term]:
            answer.append(i+1)
    
    return answer

 


 

Github

 

GitHub - bh2980/Algorithm-Problem: 알고리즘 풀이 흔적들

알고리즘 풀이 흔적들. Contribute to bh2980/Algorithm-Problem development by creating an account on GitHub.

github.com