알고리즘/기타

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

파프리카. 2023. 8. 8. 17:08

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

 

프로그래머스

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

programmers.co.kr

내 풀이

def solution(today, terms, privacies):
    ans = []
    i = 0
    today = list(map(int,today.split(".")))
    termsArr = []
    for t in terms:
        a,b = t.split(" ")
        termsArr.append([a,int(b)])
    for p in privacies:
        i+=1
        date, type = p.split(" ")
        date = list(map(int,date.split(".")))
        for t in termsArr:
            if(type==t[0]):
                date[1]+=t[1]
                if(date[1]>12):
                    n = date[1]
                    if(n%12==0):
                        date[1]=12
                        date[0]+=n//12-1
                    else:
                        date[1]=n%12
                        date[0]+=n//12
        if(today[0]>date[0]):
            ans.append(i)
        elif(today[0]==date[0] and today[1]>date[1]):
            ans.append(i)
        elif(today[0]==date[0] and today[1]==date[1] and today[2]>=date[2]):
            ans.append(i)
        print(date[0], date[1], date[2])
    return ans

다른 사람 풀이

def to_days(date):
    y,m,d = map(int,date.split("."))
    return y*12*28+m*28+d

def solution(today, terms, privacies):
    terms = {t[0]: int(t[2:])*28 for t in terms}
    ans = [ i+1 for i, p in enumerate(privacies)
            if to_days(p[:-2])+terms[p[-1]] <= to_days(today)]
    return ans

나보다 훨씬 깔끔한 풀이. yyyy.mm.dd 형태를 유지한 나와 달리 이 사람은 다 day 수로 변환하는 함수를 만들었다. 문제에서 모든 달은 28일까지 있다고 설정해주었기 때문에 쉽게 바꿀 수 있다.

 

매우 파이쏘닉(Pythonic)한 코드라고 생각한다. 보고 배워야겠다.

- 문제에 제시된 약관 종류를 딕셔너리로 처리

- for문이나 if문을 리스트 안에 넣음

- 문자열을 인덱싱하거나 map으로 계산

- enmerate함수로 for문 돌리기