From 30c88a8a383e75a6dd4e0bc9a82ff2051c4c514b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 19:03:47 +0200 Subject: [PATCH] Update and rename RPNSolver.md to solveRPN.md --- snippets/RPNSolver.md | 28 ---------------------------- snippets/solveRPN.md | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 28 deletions(-) delete mode 100644 snippets/RPNSolver.md create mode 100644 snippets/solveRPN.md diff --git a/snippets/RPNSolver.md b/snippets/RPNSolver.md deleted file mode 100644 index 4796d27cb..000000000 --- a/snippets/RPNSolver.md +++ /dev/null @@ -1,28 +0,0 @@ -### RPNSolver - -Solves the given reverse polish notation - -``` js -const RPNSolver = RPN => { - const OPERATORS = {'*' : (a,b) => a * b, '+' : (a,b) => a + b, '-' : (a,b) => a - b, '/' : (a,b) => a / b, '**': (a,b) => a ** b} - const [stack,solve] = [[],RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '')] - solve.forEach(symbol => { - if(!isNaN(parseFloat(symbol)) && isFinite(symbol)) {stack.push(symbol)} - else if (Object.keys(OPERATORS).includes(symbol)) { - const [a,b] = [stack.pop(),stack.pop()] - stack.push(OPERATORS[symbol](parseFloat(b),parseFloat(a))) - } else { throw `${symbol} is not a recognized symbol` } - } -) - if(stack.length === 1) return stack.pop() - else throw `${RPN} is not a proper RPN. Please check it and try again` -} -``` - -```js -RPNSolver('15 7 1 1 + - / 3 * 2 1 1 + + -'); // 5 -RPNSolver('3 5 6 + *'); //33 -RPNSolver('2 4 / 5 6 - *'); //-0.5 -RPNSolver('2 3 ^'); //8 -RPNSolver('2 3 ^'); //8 -``` diff --git a/snippets/solveRPN.md b/snippets/solveRPN.md new file mode 100644 index 000000000..059c0ac36 --- /dev/null +++ b/snippets/solveRPN.md @@ -0,0 +1,34 @@ +### solveRPN + +Solves the given mathematical expression in [reverse polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation). +Throws appropriate errors if there are unrecognized symbols or the expression is wrong. + +Use a dictionary, `OPERATORS` to specify each operator's matching mathematical operation. +Use `String.replace()` with a regular expression to replace `^` with `**`, `String.split()` to tokenize the string and `Array.filter()` to remove empty tokens. +Use `Array.forEach()` to parse each `symbol`, evaluate it as a numeric value or operator and solve the mathematical expression. +Numeric values are converted to floating point numbers and pushed to a `stack`, while operators are evaluated using the `OPERATORS` dictionary and pop elements from the `stack` to apply operations. + +``` js +const solveRPN = rpn => { + const OPERATORS = {'*' : (a,b) => a * b, '+' : (a,b) => a + b, '-' : (a,b) => a - b, '/' : (a,b) => a / b, '**': (a,b) => a ** b}; + const [stack,solve] = [[],rpn.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '')]; + solve.forEach(symbol => { + if(!isNaN(parseFloat(symbol)) && isFinite(symbol)) {stack.push(symbol)} + else if (Object.keys(OPERATORS).includes(symbol)) { + const [a,b] = [stack.pop(),stack.pop()]; + stack.push(OPERATORS[symbol](parseFloat(b),parseFloat(a))) + } else { throw `${symbol} is not a recognized symbol` } + } +) + if(stack.length === 1) return stack.pop() + else throw `${rpn} is not a proper RPN. Please check it and try again` +} +``` + +```js +solveRPN('15 7 1 1 + - / 3 * 2 1 1 + + -'); // 5 +solveRPN('3 5 6 + *'); //33 +solveRPN('2 4 / 5 6 - *'); //-0.5 +solveRPN('2 3 ^'); //8 +solveRPN('2 3 ^'); //8 +```