Skip to content
Merged
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
73 changes: 45 additions & 28 deletions src/_DataStructures_/Stack/postfix-expression-evaluation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,56 @@

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

const ERROR_STRING = 'Expression is not in order';

function evaluatePostfixExpression(expression) {
let s = new Stack();
for (let i = 0; i < expression.length; i++) {
const char = expression[i];
if (!isNaN(char)) {
//if number push the char onto stack
s.push(Number(char));
} else {
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
//push the result to stack
let val1 = s.pop();
let val2 = s.pop();
switch (char) {
case '+':
s.push(val2 + val1);
break;
case '-':
s.push(val2 - val1);
break;
case '*':
s.push(val2 * val1);
break;
case '/':
s.push(val2 / val1);
break;
// eslint-disable-next-line no-param-reassign
expression = expression.trim();

if (expression.length === 0 || expression.length === 1) {
throw new Error(ERROR_STRING);
}

}
}
const s = new Stack();
// eslint-disable-next-line no-plusplus
for (let i = 0; i < expression.length; i++) {
const char = expression[i];
// eslint-disable-next-line no-restricted-globals
if (!isNaN(char)) {
// if number push the char onto stack
s.push(Number(char));
} else {
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
// push the result to stack
const val1 = s.pop();
const val2 = s.pop();
switch (char) {
case '+':
s.push(val2 + val1);
break;
case '-':
s.push(val2 - val1);
break;
case '*':
s.push(val2 * val1);
break;
case '/':
s.push(val2 / val1);
break;
default:
break;
}
}
//pop the value of postfix expression
return s.pop();
}
// pop the value from stack
const result = s.pop();
if (s.isEmpty()) {
return result;
}
throw new Error(ERROR_STRING);
}

module.exports = {
evaluatePostfixExpression,
ERROR_STRING,
};
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
const { evaluatePostfixExpression } = require('.');
const { evaluatePostfixExpression, ERROR_STRING } = require('.');

describe('Postfix expression evaluation', function () {
it('should be a function', function () {
describe('Postfix expression evaluation', () => {
it('should be a function', () => {
expect(typeof evaluatePostfixExpression).toEqual('function');
});
it('should return a number', function () {

it('should return a number', () => {
const expression = '11+';
expect(typeof evaluatePostfixExpression(expression)).toEqual('number')

expect(typeof evaluatePostfixExpression(expression)).toEqual('number');
});
it('should handle addition', function () {

it('should handle addition', () => {
const expression = '23+';
const expected = 5;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle subtraction', function () {

it('should handle subtraction', () => {
const expression = '54-';
const expected = 1;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle multiplication', function () {

it('should handle multiplication', () => {
const expression = '34*';
const expected = 12;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle division', function () {

it('should handle division', () => {
const expression = '62/';
const expected = 3;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle negative numbers', function () {

it('should handle negative numbers', () => {
const expression = '25-';
const expected = -3;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle multiple operators', function () {

it('should handle multiple operators', () => {
const expression = '123*+';
const expected = 7;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});

describe('should throw error on invalid expressions', () => {
const invalidExpressions = ['12', '1', '+', '1+2', '+12'];
test.each(invalidExpressions)('running for %p', (expression) => {
expect(() => evaluatePostfixExpression(expression)).toThrow(ERROR_STRING);
});
});
});