function solution(today, terms, privacies) {
var answer = [];
privacies.map((privacy, index) => {
const [date, type] = privacy.split(" ");
const [year, month, day] = date.split(".");
const [tYear, tMonth, tDay] = today.split(".");
const duration =
Number(tYear - year) * 12 * 28 +
Number(tMonth - month) * 28 +
Number(tDay - day);
terms.map((term) => {
const [targetType, targetMonth] = term.split(" ");
if (targetType === type && duration >= targetMonth * 28) {
answer.push(index + 1);
}
});
});
return answer;
}
def solution(today, terms, privacies):
tYear, tMonth, tDay = map(int, today.split("."))
answer = []
for i in range(len(privacies)):
target, type = privacies[i].split(" ")
year, month, day = map(int, target.split("."))
term = (tYear - year) * 12 * 28 + (tMonth - month) * 28 + (tDay - day)
for j in range(len(terms)):
tarType, tarTerm = terms[j].split(" ")
if tarType == type:
if term >= int(tarTerm) * 28:
answer.append(i + 1)
return answer
회사 지원을 하면서 JS를 지원하지 않는 기업이 생각보다 많았다.
조금 다양한 풀을 지원하기 위해서 코테용 언어를 하나 배우는게 나을 것 같다고 생각해서 가장 접근이 쉬운 파이썬을 이용해 복습 겸 한번 더 푸는 방식으로 공부하자고 결정하였다.
파이썬으로 새롭게 풀면서 느꼈던 점은 구조분해할당이 JS와 비슷한 방식이어서 편했고, c++이나 java보다는 빠르게 익힐 수 있을 것 같다는 생각을 하였다.
먼저 privacies를 돌면서 split한 다음 타입에 맞춰서 기간을 체크해주면 됐다. 가볍게 풀 수 있는 구현 문제 같았다.
파이썬에서 split을 할 때 타입을 명시하지 않으면 문자열로 만들어지고, 이때 숫자 계산이 어렵기 때문에 map(type, 문자열) 메서드를 통해 타입 자체로 split하는 방법
terms을 for문을 통해 계속 반복하면서 체크하기보다는 JS의 map이나 파이썬의 dict을 통해 저장해놓고 바로 접근하면 이중 포문을 안써도 된다.
terms_dict = dict(t.split() for t in terms)