Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Evaluate-Reverse-Polish-Notation/README.md
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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why \* instead of *?


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: -
7 changes: 7 additions & 0 deletions Evaluate-Reverse-Polish-Notation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function evalRPN(tokens) {

}

module.exports = {
evalRPN
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add newline

47 changes: 47 additions & 0 deletions Evaluate-Reverse-Polish-Notation/index.test.js
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);
})
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add newline

43 changes: 43 additions & 0 deletions Evaluate-Reverse-Polish-Notation/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function evalRPN(tokens) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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++) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add line break before for loop

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
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add newline