Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 430e7f3

Browse files
postfix-expression-evaluation knaxus#61
- Update test case for exception
1 parent a00df3e commit 430e7f3

File tree

2 files changed

+78
-53
lines changed

2 files changed

+78
-53
lines changed

‎src/_DataStructures_/Stack/postfix-expression-evaluation/index.js‎

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,56 @@
66

77
const Stack = require('../index');
88

9+
const ERROR_STRING = 'Expression is not in order';
10+
911
function evaluatePostfixExpression(expression) {
10-
let s = new Stack();
11-
for (let i = 0; i < expression.length; i++) {
12-
const char = expression[i];
13-
if (!isNaN(char)) {
14-
//if number push the char onto stack
15-
s.push(Number(char));
16-
} else {
17-
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
18-
//push the result to stack
19-
let val1 = s.pop();
20-
let val2 = s.pop();
21-
switch (char) {
22-
case '+':
23-
s.push(val2 + val1);
24-
break;
25-
case '-':
26-
s.push(val2 - val1);
27-
break;
28-
case '*':
29-
s.push(val2 * val1);
30-
break;
31-
case '/':
32-
s.push(val2 / val1);
33-
break;
12+
// eslint-disable-next-line no-param-reassign
13+
expression = expression.trim();
14+
15+
if (expression.length === 0 || expression.length === 1) {
16+
return ERROR_STRING;
17+
}
3418

35-
}
36-
}
19+
const s = new Stack();
20+
// eslint-disable-next-line no-plusplus
21+
for (let i = 0; i < expression.length; i++) {
22+
const char = expression[i];
23+
// eslint-disable-next-line no-restricted-globals
24+
if (!isNaN(char)) {
25+
// if number push the char onto stack
26+
s.push(Number(char));
27+
} else {
28+
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
29+
// push the result to stack
30+
const val1 = s.pop();
31+
const val2 = s.pop();
32+
switch (char) {
33+
case '+':
34+
s.push(val2 + val1);
35+
break;
36+
case '-':
37+
s.push(val2 - val1);
38+
break;
39+
case '*':
40+
s.push(val2 * val1);
41+
break;
42+
case '/':
43+
s.push(val2 / val1);
44+
break;
45+
default:
46+
break;
47+
}
3748
}
38-
//pop the value of postfix expression
39-
return s.pop();
49+
}
50+
// pop the value from stack
51+
const result = s.pop();
52+
if (s.isEmpty()) {
53+
return result;
54+
}
55+
return ERROR_STRING;
4056
}
4157

4258
module.exports = {
4359
evaluatePostfixExpression,
60+
ERROR_STRING,
4461
};
Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,63 @@
1-
const { evaluatePostfixExpression } = require('.');
1+
const { evaluatePostfixExpression,ERROR_STRING } = require('.');
22

3-
describe('Postfix expression evaluation', function() {
4-
it('should be a function', function() {
3+
describe('Postfix expression evaluation', ()=> {
4+
it('should be a function', ()=> {
55
expect(typeof evaluatePostfixExpression).toEqual('function');
66
});
7-
8-
it('should return a number', function() {
7+
8+
it('should return a number', ()=> {
99
const expression = '11+';
10-
11-
expect(typeof evaluatePostfixExpression(expression)).toEqual('number')
10+
11+
expect(typeof evaluatePostfixExpression(expression)).toEqual('number');
1212
});
13-
14-
it('should handle addition', function() {
13+
14+
it('should handle addition', ()=> {
1515
const expression = '23+';
1616
const expected = 5;
17-
17+
1818
expect(evaluatePostfixExpression(expression)).toEqual(expected);
1919
});
20-
21-
it('should handle subtraction', function() {
20+
21+
it('should handle subtraction', ()=> {
2222
const expression = '54-';
2323
const expected = 1;
24-
24+
2525
expect(evaluatePostfixExpression(expression)).toEqual(expected);
2626
});
27-
28-
it('should handle multiplication', function() {
27+
28+
it('should handle multiplication', ()=> {
2929
const expression = '34*';
3030
const expected = 12;
31-
31+
3232
expect(evaluatePostfixExpression(expression)).toEqual(expected);
3333
});
34-
35-
it('should handle division', function() {
34+
35+
it('should handle division', ()=> {
3636
const expression = '62/';
3737
const expected = 3;
38-
38+
3939
expect(evaluatePostfixExpression(expression)).toEqual(expected);
4040
});
41-
42-
it('should handle negative numbers', function() {
41+
42+
it('should handle negative numbers', ()=> {
4343
const expression = '25-';
4444
const expected = -3;
45-
45+
4646
expect(evaluatePostfixExpression(expression)).toEqual(expected);
4747
});
48-
49-
it('should handle multiple operators', function() {
48+
49+
it('should handle multiple operators', ()=> {
5050
const expression = '123*+';
5151
const expected = 7;
52-
52+
5353
expect(evaluatePostfixExpression(expression)).toEqual(expected);
5454
});
55+
56+
describe('should throw error on invalid expressions', () => {
57+
const invalidExpressions = ['12', '1', '+', '1+2', '+12'];
58+
test.each(invalidExpressions)('running for %p', (expression) => {
59+
const result = evaluatePostfixExpression(expression);
60+
expect(result).toEqual(ERROR_STRING);
61+
});
62+
});
5563
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /