문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
내가 푼 풀이
예외 상황을 다 체크하려다 보니 복잡했다,,🤣🤣
def solution(today, terms, privacies):
answer = []
terms_month, terms_category = [], []
year, month, day, category = [], [], [], []
t_year = int(today.split('.')[0])
t_month = int(today.split('.')[1])
t_day = int(today.split('.')[2])
for i in range(len(terms)):
terms_category.append(terms[i].split(' ')[0])
terms_month.append(int(terms[i].split(' ')[1]))
for i in range(len(privacies)):
year.append(int(privacies[i].split('.')[0]))
month.append(int(privacies[i].split('.')[1]))
day.append(int(privacies[i].split('.')[2].split(' ')[0]))
category.append(privacies[i].split('.')[2].split(' ')[1])
for j in range(len(terms)):
if category[i] == terms_category[j]:
month[i] += terms_month[j]
day[i] -= 1
if day[i] == 0:
day[i] = 28
month[i] -= 1
if month[i] == 0:
month[i] = 12
year[i] -= 1
if month[i] > 12:
year[i] += month[i]//12
month[i] %= 12
if month[i] == 0:
month[i] = 12
year[i] -= 1
if year[i] < t_year:
answer.append(i+1)
elif (year[i] == t_year and month[i]<t_month):
answer.append(i+1)
elif (year[i] == t_year and month[i]==t_month and day[i]<t_day):
answer.append(i+1)
return answer
새로운 풀이
날짜를 계산할 때, 일수로 통일하면 예외 상황 생각할 필요 없이 훨씬 간단하게 풀 수 있다!
def solution(today, terms, privacies):
answer = []
t_year, t_month, t_day = map(int, today.split('.'))
today_days = t_year*12*28 + t_month*28 + t_day
terms_dict = {}
for term in terms:
category, months = term.split(' ')
terms_dict[category] = int(months)*28
for idx, val in enumerate(privacies):
date, p_category = val.split(' ')
year, month, day = map(int, date.split('.'))
start_date = year*12*28 + month*28 + day
exp_date = start_date + terms_dict[p_category] - 1
if exp_date < today_days:
answer.append(idx+1)
return answer
새로 공부한 점
1. split으로 나눈 값들을 순서대로 한 번에 담을 수 있다!
t_year, t_month, t_day = map(int, today.split('.'))
2. map() 함수를 사용해 한 번에 형변환 가능
map(function, iterable)
- function : 적용할 함수
- iterable : 함수를 적용할 데이터 집합
'문제 풀이 > Programmers' 카테고리의 다른 글
[Programmers/Python] Lv1. 체육복 (2) | 2024.10.01 |
---|---|
[Programmers/Python] Lv2. 방문 길이 (0) | 2024.06.27 |
[Programmers/Python] Lv2. 행렬의 곱셈 (0) | 2024.06.27 |
[Programmers/Python] Lv1. 실패율 (0) | 2024.06.10 |
[Programmers/Python] Lv1. 모의고사 (0) | 2024.06.07 |
댓글