코딩테스트/programmers (python)

Programmers / 1단계 / 개인정보 수집 유효기간 / python

seulll 2024. 1. 27. 17:25

 

코딩테스트 연습 - 개인정보 수집 유효기간 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

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

programmers.co.kr

 

 

나의 풀이 (테스트 실패, 정확성 65)

def solution(today, terms, privacies):
    arr = []
    dic = {}
    result = []

    for i in terms:
        arr.append(i.split())
    for i in arr:
        dic[i[0]] = int(i[1])
    for i in privacies:
        year = int(i[0:4])
        month = int(i[5:7]) + dic[i[-1]]
        if month > 12:
            month = (int(i[5:7]) + dic[i[-1]]) % 12
            year+=(int(i[5:7]) + dic[i[-1]])// 12
            if month==0:
                month=12
        day = int(i[-4:-2])
        if int(today[0:4]) > year:
            result.append(privacies.index(i) + 1)
        elif int(today[0:4]) == year and int(today[5:7]) > month:
            result.append(privacies.index(i) + 1)
        elif int(today[0:4]) == year and int(today[5:7]) == month and int(today[-2:]) >= day:
            result.append(privacies.index(i) + 1)

    return result

 

모범 답안

def timeToDay(day):
    y, m, d = map(int, day.split('.'))
    return y * 12 * 28 + m * 28 + d


def solution(today, terms, privacies):
    answer = []
    term = {}
    today = timeToDay(today)

    for t in terms:
        x, y = t.split()
        term[x] = int(y)

    for idx, p in enumerate(privacies):
        start_day, kind = p.split()
        if timeToDay(start_day) + term[kind] * 28 <= today:
            answer.append(idx+1)

    return answer

문제에서 모든 달이 28일로 고정된다는 점을 이용해 일수로 바꾸어서 계산 가능