Algorithm

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

potato_pizza 2024. 5. 21. 14:33
728x90

Programmers

개인정보 수집 유효기간

https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

프로그래머스

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

programmers.co.kr

 

문제

코드

  • privacies 리스트에서 날짜와 약관 유형을 분리해 date, tp 리스트에 저장
  • date 리스트의 각 날짜에서 split을 사용해 '.'을 제거
  • terms 리스트를 terms_dict 딕셔너리로 변환해 각 약관 유형과 기간을 저장
  • 각 privacies 항목에 대해 약관 유형의 기간을 더한 새로운 날짜를 계산. 새로운 날짜가 오늘 날짜보다 작거나 같으면 인덱스를 answer에 추가
  • fstring을 사용해 문자열 생성. :04d는 4자리 10진수, 02d는 2자리 10진수
def solution(today, terms, privacies):
    answer = []
    date, tp = [], []

    for i in privacies:
        a, b = i.split(' ')
        date.append(a)
        tp.append(b)

    for i in range(len(date)):
        date[i] = date[i].replace('.', '')

    terms_dict = {}
    for i in terms:
        term_type, duration = i.split(' ')
        terms_dict[term_type] = int(duration)

    for i in range(len(date)):
        term_type = tp[i]
        if term_type in terms_dict:
            now = date[i]
            new_month = int(now[4:6]) + terms_dict[term_type]
            new_year = int(now[:4])
            while new_month > 12:
                new_year += 1
                new_month -= 12
            new_date = f"{new_year:04d}{new_month:02d}{now[6:]}"
            if new_date <= today.replace('.', ''):
                answer.append(i + 1)

    return answer
728x90
반응형