728x90
https://school.programmers.co.kr/learn/courses/30/lessons/150370
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제를 꼼꼼히 읽고 그대로 구현만 하면 되는 문제이다.
입력의 형태가 정해져있어 슬라이싱을 이용한 하드 코딩 형태로 문제를 풀어보았다. 아니면 연,월,일을 모두 day로 변환해서 푸는 방법도 있다.
어느 쪽을 선택하던지 연/월/일을 다루는 문제는 본인이 헷갈리지 않는 방법이 최고인 것 같다!
def solution(today, terms, privacies):
answer = []
nyear = int(today[:4])
nmonth = int(today[5:7])
nday = int(today[8:])
for i in range(len(terms)):
for j in range(len(privacies)):
if terms[i][0] == privacies[j][-1]:
year = int(privacies[j][:4])
month = int(privacies[j][5:7]) + int(terms[i][2:])
day = int(privacies[j][8:10]) - 1
while month > 12:
month -= 12
year += 1
if day <= 0:
day = 28
month -= 1
if month <= 0:
month = 12
year -= 1
if nyear > year:
answer.append(j+1)
elif nyear == year:
if nmonth > month:
answer.append(j+1)
elif nmonth == month:
if nday > day:
answer.append(j+1)
answer.sort()
return answer
728x90
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/파이썬] 타겟 넘버 (0) | 2023.08.29 |
---|---|
[프로그래머스/파이썬] 정수 삼각형 (0) | 2023.05.30 |
[프로그래머스/python] Level 2 : 이모티콘 할인 행사 (0) | 2023.04.28 |
[프로그래머스/python] Level 1 : 크기가 작은 부분 문자열 (0) | 2023.01.12 |
[프로그래머스/python] Level 2 : 택배 배달과 수거하기 (3) | 2023.01.09 |