Algorithm

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

salmon16 2024. 4. 6. 16:38

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

 

프로그래머스

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

programmers.co.kr

 

풀이 방법

단순 string을 잘 처리할 수 있느냐의 문제이다.

terms를 빠르게 처리해 주기 위해 파이썬의 딕셔너리를 사용해서 각 term에 따른 개월수를 저장했다.

그 후 cal함수를 통해 term을 더해 주었다 여기서 term의 범위는 100 이하 이므로 month가 12 이하가 될 때까지 year을 더해주는 작업이 필요하다.

그 후 보관기간이 지났는지를 판단하기 위해 string으로 변경 후 string 비교를 통해 결과를 냈다.

주의할 점은 10개월 미만이냐 초과하냐에 따라 string에서 int로의 변환과정에서 주의해 주어야 한다. 

 

def solution(today, terms, privacies):
    answer = []
    dic = {}
    def cal(day_str, plus):        
        year = int(day_str[0:4])
        month = 0
        day = day_str[8:10]        
        if day_str[5] == '0': ## 10개월 미만인 경우
            month = int(day_str[6])
        else: ## 10개월 이상인 경우 
            month = int(day_str[5:7])             
        month += int(plus) ## 계산
        while(month > 12): ## month 12이하 될 때까지 year 추가해 주기
            year += 1
            month -= 12        
        if month > 9:
            return str(year) + str('.') + str(month) + str('.') + str(day)
        else:
            return str(year) + str('.0') + str(month) + str('.') + str(day)
    
    for i in range(len(terms)): ## 딕셔너리에 추가        
        dic[terms[i][0]] = int(terms[i][2:])        
        
    for i in range(len(privacies)):
        cal_day = cal(privacies[i], dic[privacies[i][-1]])         
        if cal_day <= today:
            answer.append(i+1)        
    return answer

다음에 적용할 것

  1. map, splict 함수를 사용하자
  2. 파이썬 리스트 복사에서 [시작:마지막으로 포함할 원소 뒤 인덱스]이다.
  3. 파이썬 리스트 복사에서 [:] 모든 원소 복사이다. 
  4. 파이썬 딕셔너리 문법 dic = {}, 원소 추가 dic['A'] = 123, 삭제 del dic['A']