diff --git a/Evaluate-Reverse-Polish-Notation/README.md b/Evaluate-Reverse-Polish-Notation/README.md new file mode 100644 index 0000000..9b5931d --- /dev/null +++ b/Evaluate-Reverse-Polish-Notation/README.md @@ -0,0 +1,40 @@ +# Evaluate Reverse Polish Notation + +Write a method called `evalRPN` that evaluates the value of an arithmetic expression in Reverse Polish Notation. + +Valid operators are +, -, \*, /. Each operand may be an integer or another expression. + +Notes: + +- Division between two integers should truncate toward zero (negative numbers should be rounded towards zero, and positive numbers should be rounded towards zero) +- The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation. +- [Link to Wikipedia's explanation on Reverse Polish Notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation) + +Example: + +```js +Input: ["2", "1", "+", "3", "*"]; +Output: 9; +Explanation: ((2 + 1) * 3) = 9 +``` + +```js +Input: ["4", "13", "5", "/", "+"]; +Output: 6; +Explanation: (4 + (13 / 5)) = 6 +``` + +```js +Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] +Output: 22 +Explanation: + ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 += ((10 * (6 / (12 * -11))) + 17) + 5 += ((10 * (6 / -132)) + 17) + 5 += ((10 * 0) + 17) + 5 += (0 + 17) + 5 += 17 + 5 += 22 +``` + +Companies that have asked this question: - diff --git a/Evaluate-Reverse-Polish-Notation/index.js b/Evaluate-Reverse-Polish-Notation/index.js new file mode 100644 index 0000000..78e156d --- /dev/null +++ b/Evaluate-Reverse-Polish-Notation/index.js @@ -0,0 +1,7 @@ +function evalRPN(tokens) { + +} + +module.exports = { + evalRPN +} \ No newline at end of file diff --git a/Evaluate-Reverse-Polish-Notation/index.test.js b/Evaluate-Reverse-Polish-Notation/index.test.js new file mode 100644 index 0000000..4f79987 --- /dev/null +++ b/Evaluate-Reverse-Polish-Notation/index.test.js @@ -0,0 +1,47 @@ +const { + evalRPN +} = require('./index'); + +describe("evalRPN", () => { + it("Returns an integer", () => { + const tokens = ["2", "1", "+", "3", "*"] + const result = evalRPN(tokens); + expect(typeof (result)).toBe('number'); + }); + + it("Truncates division between two integers toward zero when negative", () => { + const tokens = ["2", "-123", "/"]; + const result = evalRPN(tokens); + expect(result).toEqual(0); + }) + + it("Truncates division between two integers toward zero when positive", () => { + const tokens = ["4", "3", "/"]; + const result = evalRPN(tokens); + expect(result).toEqual(1); + }); + + it("Evaluates correctly for ['2', '1', '+', '3', '*']", () => { + const tokens = ["2", "1", "+", "3", "*"]; + const result = evalRPN(tokens); + expect(result).toEqual(9) + }) + + it("Evaluates correctly for ['4', '13', '5', '/', '+']", () => { + const tokens = ["4", "13", "5", "/", "+"]; + const result = evalRPN(tokens); + expect(result).toEqual(6) + }) + + it("Evaluates correctly for ['10', '6', '9', '3', '+', '-11', '*', '/', '*', '17', '+', '5', '+']", () => { + const tokens = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]; + const result = evalRPN(tokens); + expect(result).toEqual(22) + }); + + it("Returns integer if tokens have length of 1", () => { + const tokens = ["13"]; + const result = evalRPN(tokens); + expect(result).toEqual(13); + }) +}) \ No newline at end of file diff --git a/Evaluate-Reverse-Polish-Notation/solution.js b/Evaluate-Reverse-Polish-Notation/solution.js new file mode 100644 index 0000000..06df1f5 --- /dev/null +++ b/Evaluate-Reverse-Polish-Notation/solution.js @@ -0,0 +1,43 @@ +function evalRPN(tokens) { + let stack = []; + let total; + let operators = { + "+": function (a, b) { + return a + b + }, + "-": function (a, b) { + return a - b + }, + "*": function (a, b) { + return a * b + }, + "/": function (a, b) { + let number = a / b + let quotient = Math[number < 0 ? 'ceil' : 'floor'](number); + if (quotient === -0) { + return 0 + } else { + return quotient + } + } + } + + if (tokens.length === 1) { + return parseInt(tokens[0]); + } + for (let i = 0; i < tokens.length; i++) { + if (tokens[i] in operators) { + let second = stack.pop(); + let first = stack.pop(); + let amount = operators[tokens[i]](first, second); + stack.push(amount); + } else { + stack.push(parseInt(tokens[i])); + } + } + return stack.pop(); +}; + +module.exports = { + evalRPN +} \ No newline at end of file