출처 : https://school.programmers.co.kr/learn/courses/30/lessons/150370
풀이 방법
단순 string을 잘 처리할 수 있느냐의 문제이다.
terms를 빠르게 처리해 주기 위해 파이썬의 딕셔너리를 사용해서 각 term에 따른 개월수를 저장했다.
그 후 cal함수를 통해 term을 더해 주었다 여기서 term의 범위는 100 이하 이므로 month가 12 이하가 될 때까지 year을 더해주는 작업이 필요하다.
그 후 보관기간이 지났는지를 판단하기 위해 string으로 변경 후 string 비교를 통해 결과를 냈다.
주의할 점은 10개월 미만이냐 초과하냐에 따라 string에서 int로의 변환과정에서 주의해 주어야 한다.
def solution(today, terms, privacies):
answer = []
dic = {}
def cal(day_str, plus):
year = int(day_str[0:4])
month = 0
day = day_str[8:10]
if day_str[5] == '0': ## 10개월 미만인 경우
month = int(day_str[6])
else: ## 10개월 이상인 경우
month = int(day_str[5:7])
month += int(plus) ## 계산
while(month > 12): ## month 12이하 될 때까지 year 추가해 주기
year += 1
month -= 12
if month > 9:
return str(year) + str('.') + str(month) + str('.') + str(day)
else:
return str(year) + str('.0') + str(month) + str('.') + str(day)
for i in range(len(terms)): ## 딕셔너리에 추가
dic[terms[i][0]] = int(terms[i][2:])
for i in range(len(privacies)):
cal_day = cal(privacies[i], dic[privacies[i][-1]])
if cal_day <= today:
answer.append(i+1)
return answer
다음에 적용할 것
- map, splict 함수를 사용하자
- 파이썬 리스트 복사에서 [시작:마지막으로 포함할 원소 뒤 인덱스]이다.
- 파이썬 리스트 복사에서 [:] 모든 원소 복사이다.
- 파이썬 딕셔너리 문법 dic = {}, 원소 추가 dic['A'] = 123, 삭제 del dic['A']
'Algorithm' 카테고리의 다른 글
[백준] 욕심쟁이 판다 1937번 (Java) (0) | 2024.04.11 |
---|---|
[프로그래머스] 이모티콘 할인행사 (Java) (0) | 2024.04.10 |
[프로그래머스] 등산코스 정하기 (다익스트라) (python) (0) | 2024.04.02 |
[백준] 수들의 합 2 2003번 (python) 투 포인터 (1) | 2024.03.24 |
[백준] A->B 16953번 (python) (0) | 2024.03.22 |