-
Notifications
You must be signed in to change notification settings - Fork 12
Reverse polish notation #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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: - | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| function evalRPN(tokens) { | ||
|
|
||
| } | ||
|
|
||
| module.exports = { | ||
| evalRPN | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add newline |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't neglect your code formatter! missing semicolon here, also inconsistent use of single vs double quotes etc. |
||
| }) | ||
|
|
||
| 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); | ||
| }) | ||
| }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add newline |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| function evalRPN(tokens) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. run this file through prettier too. |
||
| 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++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add line break before |
||
| 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 | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add newline |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
\*instead of*?