2023 KAKAO BLIND RECRUITMENT - 개인정보 수집 유효기간(Lv1, JS)

j_wisdom_h·2023년 2월 2일
0

CodingTest

목록 보기
32/58

2023 KAKAO BLIND RECRUITMENT - 개인정보 수집 유효기간(Lv1, JS)

문제설명


입출력 예 & 제한사항

Solution

1차 솔루션

//2023 KAKAO BLIND RECRUITMENT - 개인정보 수집 유효기간

function solution(today, terms, privacies) {
	const perMonth = 28;
	const [Tyear, Tmonth, Tday] = today.split('.').map(Number); 
    // today year, month, day

    const termObj = terms.reduce((res, item) => {
		const [kind, duration] = item.split(' ');
		res[kind] = parseInt(duration);
		return res;
	}, {});

	const p_split = privacies.map((p) => p.split(/\.| /));
    
    const validityDate = p_split.map((p) => {
        let [year,month,day,kind] = [...p];
        const duration = termObj[kind];

        year = parseInt(year);
        month = parseInt(month);
        day = parseInt(day);
        
        const after = month + duration;
        const afterYear = parseInt(after/12);
        const afterMonth = after % 12;

        if (after > 12) {
            year += afterYear;
            month = afterMonth;
            if (month === 0) {year--; month = 12;}
        } else {
            month = after;
        }

        if (day === 1) {
            month--;
            day = perMonth;
        } else {
            day--;
        }
        return [year,month,day];
    });
    
    const result = validityDate.reduce((acc,cur,idx) => {
        const [Vyear, Vmonth, Vday] = [...cur];   

        if(Vyear < Tyear) { 
            acc.push(idx+1);
        }
        else if(Vyear === Tyear){
            if(Vmonth < Tmonth){acc.push(idx + 1)}
            else if(Vmonth === Tmonth){
                if(Vday < Tday){acc.push(idx+1)}
            }
        }
        return acc;
    },[]);

    return result;
}

solution(today, terms, privacies)


2차 솔루션 ( 코드 줄이기)

function solution(today, terms, privacies) {
    const result = [];
	const [Tyear, Tmonth, Tday] = today.split('.').map(Number);
	const termObj = terms.reduce((res, item) => {
		const [kind, duration] = item.split(' ');
		res[kind] = parseInt(duration);
		return res;
	}, {});

	privacies.map((p,idx) => {
		const [date, kind] = p.split(' ');
		const duration = termObj[kind];
		let [Vyear, Vmonth, Vday] = [...date.split('.').map(Number)];

		Vmonth += duration;
		Vyear +=
			Vmonth % 12 === 0  
           	? parseInt(Vmonth / 12) - 1 
            : parseInt(Vmonth / 12);
		Vmonth = Vmonth % 12 === 0 ? 12 : Vmonth % 12;
        
        if (Vyear < Tyear) {
            result.push(idx + 1);
        } else if (Vyear === Tyear && Vmonth < Tmonth) {
            result.push(idx + 1);
		} else if (Vyear === Tyear && Vmonth === Tmonth && Vday <= Tday) {
		    result.push(idx + 1);
		}
	});
	return result;
}

3차 솔루션(날(day)로 계산하기)

function solution(today, terms, privacies) {
	const perMonth = 28;
	const [Tyear, Tmonth, Tday] = today.split('.').map(Number); 

	const todates = Tyear * 12 * 28 + Tmonth * 28 + Tday;

	const termObj = terms.reduce((res, item) => {
		const [kind, duration] = item.split(' ');
		res[kind] = parseInt(duration);
		return res;
	}, {});

	const result = privacies.reduce((acc,cur,idx) => {
		const [date, kind] = cur.split(' ');
		const duration = termObj[kind];
		const [year, month, day] = [...date.split('.').map(Number)];
        const dates = year * 12 * 28 + month * 28 + day + duration * 28;
        if (dates <= todates) acc.push(idx+1);
        return acc;
	},[]);

	return result;
}


profile
뚜잇뚜잇 FE개발자

0개의 댓글