출처 : 프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/150370
더보기
먼저, 생각해야 할 부분
1. 어떻게 해야 기간이 지났다는 것을 명확하게 알 수 있을까?
년, 월, 일을 일일이 비교하는 것보다 두 날짜 사이의 일자를 구해서 하나의 정수로 비교하는게 쉽다.
2. 유효기간과 지난 일자가 같을 경우와 같은 엣지 케이스에 주의하자.
풀이
from collections import defaultdict
def solution(today, terms, privacies):
answer = []
t_year, t_month, t_date = map(int, today.split('.'))
terms_detail = defaultdict(int)
for term in terms:
policy, due_month = map(lambda x: x if x.isalpha() else int(x), term.split())
terms_detail[policy] = due_month * 28
for i in range(len(privacies)):
privacy = privacies[i]
start_day, term = privacy.split()
year, month, date = map(int, start_day.split('.'))
diff_year = t_year - year
diff_month = t_month - month
diff_date = t_date - date
total_diff = diff_year * 28 * 12 + diff_month * 28 + diff_date
if total_diff >= terms_detail[term]:
answer.append(i+1)
return answer
Github
'알고리즘 > Python' 카테고리의 다른 글
[python] 프로그래머스 - 튜플 (0) | 2023.03.09 |
---|---|
[python] 프로그래머스 - 멀리 뛰기 (1) | 2023.03.09 |
[python] 프로그래머스 - 성격 유형 검사하기 (0) | 2023.03.09 |
[python] 프로그래머스 - 최솟값 만들기 (0) | 2022.07.17 |
[python] 프로그래머스 - 숫자 블록 (0) | 2022.07.15 |