본문 바로가기
Dev/Python

[PS][python]개인정보 수집 유효기간

by E.Clone 2023. 9. 13.

제목: 개인정보 수집 유효기간(2023 KAKAO BLIND RECRUITMENT)

난이도: Lv.1

사용언어: Python

def solution(today, terms, privacies):
    
    answer = []
    
    today = [int(i) for i in today.split('.')]
    
    terms_dict = {key: int(value) for key, value in (item.split() for item in terms)}
    
    for data_n in range(len(privacies)):
        
        data = privacies[data_n]
        term_type = data.split()[1]
        
        date = [int(i) for i in data.split()[0].split('.')]
        date[1] += terms_dict[term_type]
        
        today_value = today[0]*28*12 + today[1]*28 + today[2]
        date_value = date[0]*28*12 + date[1]*28 + date[2]
        
        if(today_value >= date_value):
            answer.append(data_n+1)
        
    return answer

「풀이방식」

1) 개인정보 수집 일자의 '월' 수치에 유효기간을 더하여 만료일자에 저장한다.

2) 오늘날짜의 '연도', '월', '일' 에 각각 (28*12), (12), (1) 의 가중치를 곱한 값과 만료일자에도 동일한 가중치를 곱하여 두 값을 비교, 만료일자에 가중치를 곱한 값보다 오늘날짜에 가중치를 곱한 값이 같거나 크다면 answer 리스트에 추가한다.


「검색했던 내용」

1) 리스트 컴프리헨션

today = [int(i) for i in today.split('.')]
terms_dict = {key: int(value) for key, value in (item.split() for item in terms)}

이와 같이 반복문을 사용하는 것보다 코드를 간결하게 작성할 수 있고, 성능적으로 효율적일 수 있다. 리스트 컴프리헨션은 다양한 작업에 사용될 수 있으며, 데이터 변환, 필터링 및 초기화 작업에 유용하게 활용된다.

또한 아래와 같이 다양한 컴프리헨션이 있다.

 

딕셔너리 컴프리헨션: 딕셔너리를 생성하고 초기화하기 위한 컴프리헨션. 리스트 컴프리헨션과 유사하지만 중괄호 {}를 사용한다.

data = {"A": 1, "B": 2, "C": 3}
squared_data = {key: value ** 2 for key, value in data.items()}

집합 컴프리헨션: 집합을 생성하고 초기화하기 위한 컴프리헨션. 중괄호 {}를 사용하며 중복된 항목이 없는 집합을 만든다.

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = {x for x in numbers}

문자열 컴프리헨션: 문자열을 생성하는 데 사용.

characters = ['a', 'b', 'c']
text = ''.join([c.upper() for c in characters])

제너레이터 컴프리헨션: 제너레이터를 생성하는 데 사용. 제너레이터는 한 번에 하나의 항목을 생성하는 데 유용하다.

numbers = [1, 2, 3, 4, 5]
squared_generator = (x ** 2 for x in numbers)

 

반응형