Skip to content

feat: add algorithm to evaluate postfix string #1441

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

Merged
merged 11 commits into from
Oct 10, 2023
66 changes: 66 additions & 0 deletions Data-Structures/Stack/EvaluateExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Evaluate a numeric operations string in postfix notation using a stack.
* Supports basic arithmetic operations: +, -, *, /
* Literature reference: https://www.geeksforgeeks.org/evaluation-of-postfix-expression/
*
* @param {string} expression - Numeric operations expression to evaluate.
* @returns {number|null} - Result of the expression evaluation, or null if the expression is invalid.
*/
function evaluatePostfixExpression(expression) {
const stack = [];

// Helper function to perform an operation and push the result to the stack
function performOperation(operator) {
const operand2 = stack.pop();
const operand1 = stack.pop();
if (operator === '+' || operator === '-' || operator === '*' || operator === '/') {
if (operand1 === undefined || operand2 === undefined) {
return null; // Invalid expression
}
switch (operator) {
case '+':
stack.push(operand1 + operand2);
break;
case '-':
stack.push(operand1 - operand2);
break;
case '*':
stack.push(operand1 * operand2);
break;
case '/':
if (operand2 === 0) {
return null; // Division by zero
}
stack.push(operand1 / operand2);
break;
}
} else {
return null; // Invalid operator
}
}

const tokens = expression.split(' ');

for (const token of tokens) {
if (!isNaN(parseFloat(token))) {
// If the token is a number, push it to the stack
stack.push(parseFloat(token));
} else {
// If the token is an operator, perform the operation
const result = performOperation(token);
if (result === null) {
return null; // Invalid expression
}
}
}

if (stack.length === 1) {
// The final result should be on the stack
return stack[0];
} else {
return null; // Invalid expression
}
}

export { evaluatePostfixExpression };

22 changes: 22 additions & 0 deletions Data-Structures/Stack/test/EvaluateExpression.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { evaluatePostfixExpression } from '../EvaluateExpression.js';

describe('evaluatePostfixExpression', () => {
it('should evaluate a valid expression', () => {
const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 11
const result = evaluatePostfixExpression(expression);
expect(result).toBe(11);
});

it('should handle division by zero', () => {
const expression = '3 0 /'; // Division by zero
const result = evaluatePostfixExpression(expression);
expect(result).toBe(null);
});

it('should handle an invalid expression', () => {
const expression = '3 * 4 2 / +'; // Invalid expression
const result = evaluatePostfixExpression(expression);
expect(result).toBe(null);
});

});