https://programmers.co.kr/learn/courses/30/lessons/67257
전위표기식 -> 후위 표기식 -> 계산
const getPostfix = (expression, priority) => {
const st = [], cal = [];
var val = 0;
for (var c of expression) {
if ('0' <= c && c <= '9') {
val = 10 * val + Number(c);
continue;
}
cal.push(val);
val = 0;
while (st.length && priority[st[st.length - 1]] >= priority[c])
cal.push(st.pop());
st.push(c);
}
if (val) cal.push(val)
return cal.concat(st.reverse());
}
const getAnswer = (cal) => {
const nums = [];
cal.forEach(v => {
if (!isNaN(v)) return nums.push(v);
const b = nums.pop();
const a = nums.pop();
switch (v) {
case '+':
return nums.push(a + b);
case '-':
return nums.push(a - b);
case '*':
return nums.push(a * b);
}
})
return nums.pop();
}
function solution(expression) {
const getMax = () => {
var res = getAnswer(getPostfix(expression, priority));
res = res > 0 ? res : -res
return max > res ? max : res;
}
var priority = { '+': 0, '-': 1, '*': 2 }
var max = getMax();
priority = { '+': 1, '-': 0, '*': 2 }
max = getMax();
priority = { '+': 0, '-': 2, '*': 1 }
max = getMax();
priority = { '+': 1, '-': 2, '*': 0 }
max = getMax();
priority = { '+': 2, '-': 1, '*': 0 }
max = getMax();
priority = { '+': 2, '-': 0, '*': 1 }
max = getMax();
return max;
}
문제해결에 급급하다보니.. 좀 아쉬운 부분이 많다.
const getPostfix = (expressionArr, priority) => {
const st = [], cal = [];
expressionArr.forEach(item => {
if (!isNaN(item))
return cal.push(Number(item));
while (st.length && priority[st[st.length - 1]] >= priority[item])
cal.push(st.pop());
st.push(item);
})
return cal.concat(st.reverse());
}
function solution(expression) {
const priors = [
{ '+': 0, '-': 1, '*': 2 },
{ '+': 0, '-': 2, '*': 1 },
{ '+': 1, '-': 0, '*': 2 },
{ '+': 2, '-': 0, '*': 1 },
{ '+': 1, '-': 2, '*': 0 },
{ '+': 2, '-': 1, '*': 0 },
]
const calculate = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b
}
return Math.max(...priors.map(prior => {
const nums = [];
getPostfix(expression.split(/(\D)/), prior).forEach(v => {
if (!isNaN(v)) return nums.push(v);
const b = nums.pop();
const a = nums.pop();
nums.push(calculate[v](a, b));
})
return Math.abs(nums.pop());
}));
}
function solution(expression) {
const priors = [
['-', '*', '+'],
['-', '+', '*'],
['*', '-', '+'],
['*', '+', '-'],
['+', '-', '*'],
['+', '*', '-']
]
const calculate = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b
}
return Math.max(...priors.map(prior => {
const arr = expression.split(/(\D)/);
for (let op of prior) {
while (arr.includes(op)) {
const idx = arr.indexOf(op);
const cal = arr.slice(idx - 1, idx + 2);
const res = calculate[op](Number(cal[0]), Number(cal[2]));
arr.splice(idx - 1, 3, res);
}
}
return Math.abs(arr[0]);
}))
}