관리 메뉴

JDevBlog

Day5_코딩 기초 트레이닝_프로그래머스 본문

Software Engineering/Algorithm Test

Day5_코딩 기초 트레이닝_프로그래머스

Jay___Kim 2023. 7. 16. 10:24

코드 처리하기

description: https://school.programmers.co.kr/learn/courses/30/lessons/181932

 

My answer was 

function solution(code) {
    let answer = '';
    let mode = 0;
    
    for(let idx = 0; idx < code.length; idx++) {
        const firstCondition = !mode && (idx % 2 === 0)
        const secondCondition = mode && (idx % 2 === 1)        
        if(code[idx] !== '1' && (firstCondition || secondCondition)) answer += code[idx];
        mode = code[idx] === '1' ? mode ? 0 : 1 : mode
    } 
    return answer || "EMPTY";
}

 

Best answer was 

function solution(code) {
    let answer = '';
    let mode = 0;

    for (let i = 0; i < code.length; i += 1) {
      if (Number(code[i]) === 1) {
        mode = mode === 1 ? 0 : 1;
      }
      if (Number(code[i]) !== 1 && i % 2 === mode) {
        answer += code[i];
      }
    }
    return answer.length > 0 ? answer : 'EMPTY';
}

 

주사위 게임

My answer was

// Solution1

function solution(a, b, c) {

    const allDifferent = a !== b && b !== c && a !== c;
    const oneDifferent = (a === b && b !== c) || (b === c && a !== b) || (a === c && a !== b);
    const allIdentical = a === b && b === c;

    const total = a + b + c;
    const multipleTotal = Math.pow(a, 2) + Math.pow(b, 2) + Math.pow(c, 2);
    const tripleTotal = Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3);

    return allDifferent ? total : oneDifferent ? total * multipleTotal : total * multipleTotal * tripleTotal;
}

// Solution2

function solution(a, b, c) {    
    const sum1 = a + b + c;
    const sum2 = a * a + b * b + c * c
    const sum3 = a ** 3 + b ** 3 + c ** 3
    
    if(a === b && b === c) return sum1 * sum2 * sum3;
    else if((a === b)||(b === c)||(a === c)) return sum1 * sum2;
    else return sum1;

 

Best answer was

const solution = (a, b, c) => {
    const set = new Set([a, b, c]);
    switch ([...set].length) {
        case 1: return calculate([a, b, c], 3);
        case 2: return calculate([a, b, c], 2);
        case 3: return calculate([a, b, c]);
    }
};

const calculate = (inc, n=1) => {
    const [a, b, c] = inc;
    let result = 1;
    for (let i = 1; i <= n; i++) {
        result *= Math.pow(a, i) + Math.pow(b, i) + Math.pow(c, i)
    }

    return result;
};

const set = new Set([a, b, c]) 을 사용하여 중복된 인자의 개수를 파악한 점이 좋다.

calculate 대신, sum1, sum2, sum3 를 그냥 리턴하는 것이 좋겠다. 

 

원소들의 곱과 합

My answer was

function solution(num_list) {    
    const multiplied = num_list.reduce((acc, cur) => acc * cur, 1);
    const squaredSummed = num_list.reduce((acc, cur) => acc + cur, 0) ** 2
    
    return +(multiplied < squaredSummed)
}

Best answer was

function solution(num_list) {
    let accMul = 1
    let accSum = 0
    for (const num of num_list) {
        accMul *= num
        accSum += num
    }
    return accMul < accSum ** 2 ? 1 : 0
}

for 구문을 한번만 돌려서 시간 복잡도를 낮춘 것이 훌륭하다.

 

이어 붙인 수

My answer was

function solution(num_list) {
    let odd = ''
    let even = ''
    
    for(const n of num_list) {
       if(n % 2 === 0) even += n
       else odd += n
    }
    
    return Number(odd) + Number(even)
}

Best answer was

새롭다.

Comments