From a31f6b6f206b0b07e889de01808eff00d905ab4f Mon Sep 17 00:00:00 2001 From: maxnawa31 Date: Tue, 9 Oct 2018 14:29:17 -0700 Subject: [PATCH 1/2] reverse polish notation in progress --- Evaluate-Reverse-Polish-Notation/README.md | 38 ++++++++++++++++ Evaluate-Reverse-Polish-Notation/index.js | 4 ++ .../index.test.js | 41 ++++++++++++++++++ Evaluate-Reverse-Polish-Notation/solution.js | 43 +++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 Evaluate-Reverse-Polish-Notation/README.md create mode 100644 Evaluate-Reverse-Polish-Notation/index.js create mode 100644 Evaluate-Reverse-Polish-Notation/index.test.js create mode 100644 Evaluate-Reverse-Polish-Notation/solution.js diff --git a/Evaluate-Reverse-Polish-Notation/README.md b/Evaluate-Reverse-Polish-Notation/README.md new file mode 100644 index 0000000..a5aa451 --- /dev/null +++ b/Evaluate-Reverse-Polish-Notation/README.md @@ -0,0 +1,38 @@ +# 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 +``` diff --git a/Evaluate-Reverse-Polish-Notation/index.js b/Evaluate-Reverse-Polish-Notation/index.js new file mode 100644 index 0000000..75c9742 --- /dev/null +++ b/Evaluate-Reverse-Polish-Notation/index.js @@ -0,0 +1,4 @@ +function evalRPN(tokens) { + +} +export default 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..08a5b85 --- /dev/null +++ b/Evaluate-Reverse-Polish-Notation/index.test.js @@ -0,0 +1,41 @@ +const { + evalRPN +} = require('./solution'); + +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", () => { + const tokens = ["2", "1", "+", "3", "*"]; + const result = evalRPN(tokens); + expect(result).toEqual(9) + }) + + it("Evaluates correctly", () => { + const tokens = ["4", "13", "5", "/", "+"]; + const result = evalRPN(tokens); + expect(result).toEqual(6) + }) + + it("Evaluates correctly", () => { + const tokens = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]; + const result = evalRPN(tokens); + expect(result).toEqual(22) + }) +}) \ 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 From a848e6374e66caa083bdfa4bc51cf51c989b27a2 Mon Sep 17 00:00:00 2001 From: maxnawa31 Date: Tue, 9 Oct 2018 14:35:16 -0700 Subject: [PATCH 2/2] finished reverse polish notation, ready for pull request --- Evaluate-Reverse-Polish-Notation/README.md | 2 ++ Evaluate-Reverse-Polish-Notation/index.js | 5 ++++- Evaluate-Reverse-Polish-Notation/index.test.js | 14 ++++++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Evaluate-Reverse-Polish-Notation/README.md b/Evaluate-Reverse-Polish-Notation/README.md index a5aa451..9b5931d 100644 --- a/Evaluate-Reverse-Polish-Notation/README.md +++ b/Evaluate-Reverse-Polish-Notation/README.md @@ -36,3 +36,5 @@ Explanation: = 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 index 75c9742..78e156d 100644 --- a/Evaluate-Reverse-Polish-Notation/index.js +++ b/Evaluate-Reverse-Polish-Notation/index.js @@ -1,4 +1,7 @@ function evalRPN(tokens) { } -export default evalRPN; \ No newline at end of file + +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 index 08a5b85..4f79987 100644 --- a/Evaluate-Reverse-Polish-Notation/index.test.js +++ b/Evaluate-Reverse-Polish-Notation/index.test.js @@ -1,6 +1,6 @@ const { evalRPN -} = require('./solution'); +} = require('./index'); describe("evalRPN", () => { it("Returns an integer", () => { @@ -21,21 +21,27 @@ describe("evalRPN", () => { expect(result).toEqual(1); }); - it("Evaluates correctly", () => { + it("Evaluates correctly for ['2', '1', '+', '3', '*']", () => { const tokens = ["2", "1", "+", "3", "*"]; const result = evalRPN(tokens); expect(result).toEqual(9) }) - it("Evaluates correctly", () => { + it("Evaluates correctly for ['4', '13', '5', '/', '+']", () => { const tokens = ["4", "13", "5", "/", "+"]; const result = evalRPN(tokens); expect(result).toEqual(6) }) - it("Evaluates correctly", () => { + 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