코딩테스트 문제풀이/프로그래머스

[프로그래머스] 수식 최대화

itaeiou 2021. 9. 29. 18:09
반응형

https://programmers.co.kr/learn/courses/30/lessons/67257

 

코딩테스트 연습 - 수식 최대화

IT 벤처 회사를 운영하고 있는 라이언은 매년 사내 해커톤 대회를 개최하여 우승자에게 상금을 지급하고 있습니다. 이번 대회에서는 우승자에게 지급되는 상금을 이전 대회와는 다르게 다음과

programmers.co.kr

 

 

결과 코드

function solution(expression) {
    let answer = [];
    let number_arr = expression.split(/[+\-\*]/);
    let operator_arr = expression.replace(/[^+\-\*]/g, "").split("");
    
    answer.push(solve(number_arr.slice(), operator_arr.slice(), ["+", "-", "*"]));
    answer.push(solve(number_arr.slice(), operator_arr.slice(), ["+", "*", "-"]));
    answer.push(solve(number_arr.slice(), operator_arr.slice(), ["-", "+", "*"]));
    answer.push(solve(number_arr.slice(), operator_arr.slice(), ["-", "*", "+"]));
    answer.push(solve(number_arr.slice(), operator_arr.slice(), ["*", "+", "-"]));
    answer.push(solve(number_arr.slice(), operator_arr.slice(), ["*", "-", "+"]));
        
    return Math.max.apply(null, answer);
}

const solve = (number_param, operator_param, selected_operator) => {    
    selected_operator.forEach((selected_oper) => {
        let number_arr = [];
        let operator_arr = [];

        operator_param.forEach((v, i) => {
            let number = number_param[i];
            let operator = operator_param[i];

            if(operator == selected_oper) {
                number_param[i+1] = eval(number + operator + number_param[i+1]);
            } else {
                number_arr.push(number);
                operator_arr.push(operator);
            }
        })
        number_arr.push(number_param[number_param.length-1]);

        number_param = number_arr;
        operator_param = operator_arr;
    })
    
    return Math.abs(number_param[0]);
}
// 리팩토링 이전

function solution(expression) {
    let answer = [];
    let number_arr = expression.split(/[+\-\*]/);
    let operator_arr = expression.replace(/[^+\-\*]/g, "").split("");
    
    answer.push(Math.abs(func(func(func([number_arr,operator_arr], "+"), "-"), "*")[0]));
    answer.push(Math.abs(func(func(func([number_arr,operator_arr], "+"), "*"), "-")[0]));
    answer.push(Math.abs(func(func(func([number_arr,operator_arr], "-"), "+"), "*")[0]));
    answer.push(Math.abs(func(func(func([number_arr,operator_arr], "-"), "*"), "+")[0]));
    answer.push(Math.abs(func(func(func([number_arr,operator_arr], "*"), "+"), "-")[0]));
    answer.push(Math.abs(func(func(func([number_arr,operator_arr], "*"), "-"), "+")[0]));
        
    return Math.max.apply(null, answer);
}

const func = (params, selected_operator) => {
    let number_param = params[0].slice();
    let operator_param = params[1].slice();
    
    let number_arr = [];
    let operator_arr = [];
    
    operator_param.forEach((v, i) => {
        let number = number_param[i];
        let operator = operator_param[i];
        
        if(operator == selected_operator) {
            number_param[i+1] = eval(number + operator + number_param[i+1]);
        } else {
            number_arr.push(number);
            operator_arr.push(operator);
        }
    })
    number_arr.push(number_param[number_param.length-1]);
    
    return [number_arr, operator_arr];
}
반응형