Skip to content

Instantly share code, notes, and snippets.

@kavitshah8
Last active October 9, 2020 06:49
Show Gist options
  • Save kavitshah8/6ba268c9185c6d7ba3a41e1fbf34be9d to your computer and use it in GitHub Desktop.
Save kavitshah8/6ba268c9185c6d7ba3a41e1fbf34be9d to your computer and use it in GitHub Desktop.
Stack Based Interview Questions
function RPN (seq) {
if (seq.length <= 2) {
console.log('Please enter valid RPN');
return;
}
let operands = ['+', '-', '*', '/' ],
stack = [],
i = 0;
stack.push(seq[i]);
i++;
while (i <= seq.length) {
let item = seq[i];
if (isNaN(item)) {
let operandIndex = operands.indexOf(item);
if (operandIndex == 0) {
// pop the stack by removing the last element
// splice mutates the array
// let a = parseInt(stack.splice(-1)[0], 10),
let a = parseInt(stack.pop(), 10),
b = parseInt(stack.pop(), 10);
stack.push(a+b);
}
if (operandIndex == 1) {
let a = parseInt(stack.pop(), 10),
b = parseInt(stack.pop(), 10);
stack.push(b-a)
}
if (operandIndex == 2) {
let a = parseInt(stack.pop(), 10),
b = parseInt(stack.pop(), 10);
stack.push(a*b)
}
if (operandIndex == 3) {
let a = parseInt(stack.pop(), 10),
b = parseInt(stack.pop(), 10);
stack.push(b/a)
}
} else {
stack.push(parseInt(item, 10));
}
i++
}
return stack[0];
};
console.log(RPN(["2", "1", "+", "3", "*"])) // 9
console.log(RPN(["4", "13", "5", "/", "+"])) // 6
console.log(RPN(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"])) // 22
console.log(RPN(["2", "1"])) // Please enter valid RPN undefined
@TNT-RoX
Copy link

TNT-RoX commented Apr 17, 2019

or...

const postfix = input => input.reduce((result, token) => 
 isNaN(token)
  ? [ eval(`${result.shift()}${token}${result.shift()}`), ...result ] 
  : [ token, ...result ]
, [])[0];

should work )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment