From 032b28f827cc6559b168832629a7a42013c982ea Mon Sep 17 00:00:00 2001 From: gaurovgiri Date: Thu, 5 Oct 2023 14:11:43 +0545 Subject: [PATCH 01/10] feat: add algorithm to evaluate postfix strings --- Data-Structures/Stack/EvaluateExpression.js | 65 +++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Data-Structures/Stack/EvaluateExpression.js diff --git a/Data-Structures/Stack/EvaluateExpression.js b/Data-Structures/Stack/EvaluateExpression.js new file mode 100644 index 0000000000..e6d5dd1b96 --- /dev/null +++ b/Data-Structures/Stack/EvaluateExpression.js @@ -0,0 +1,65 @@ +/** + * Evaluate a numeric operations string using a stack. + * Supports basic arithmetic operations: +, -, *, / + * + * @param {string} expression - Numeric operations expression to evaluate. + * @returns {number|null} - Result of the expression evaluation, or null if the expression is invalid. + */ +function evaluateExpression(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 { evaluateExpression }; + \ No newline at end of file From 83fae7502f26ff2996b1c4c4ea15b01ee95fcff9 Mon Sep 17 00:00:00 2001 From: gaurovgiri Date: Thu, 5 Oct 2023 14:12:45 +0545 Subject: [PATCH 02/10] feat: add test case for evaluate expression --- .../Stack/test/EvaluateExpression.test.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Data-Structures/Stack/test/EvaluateExpression.test.js diff --git a/Data-Structures/Stack/test/EvaluateExpression.test.js b/Data-Structures/Stack/test/EvaluateExpression.test.js new file mode 100644 index 0000000000..55c8bf1747 --- /dev/null +++ b/Data-Structures/Stack/test/EvaluateExpression.test.js @@ -0,0 +1,22 @@ +import { evaluateExpression } from '../evaluateExpression.js'; + +describe('evaluateExpression', () => { + it('should evaluate a valid expression', () => { + const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 7 + const result = evaluateExpression(expression); + expect(result).toBe(7); + }); + + it('should handle division by zero', () => { + const expression = '3 0 /'; // Division by zero + const result = evaluateExpression(expression); + expect(result).toBe(null); + }); + + it('should handle an invalid expression', () => { + const expression = '3 * 4 2 / +'; // Invalid expression + const result = evaluateExpression(expression); + expect(result).toBe(null); + }); + +}); From ed1b831fe0549a1f552985ed19a22b3f8520be5b Mon Sep 17 00:00:00 2001 From: gaurovgiri Date: Thu, 5 Oct 2023 14:16:28 +0545 Subject: [PATCH 03/10] update: add literature reference --- Data-Structures/Stack/EvaluateExpression.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Data-Structures/Stack/EvaluateExpression.js b/Data-Structures/Stack/EvaluateExpression.js index e6d5dd1b96..c7e3b6600f 100644 --- a/Data-Structures/Stack/EvaluateExpression.js +++ b/Data-Structures/Stack/EvaluateExpression.js @@ -1,6 +1,7 @@ /** * Evaluate a numeric operations string 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. From 0f418b0aca49be98402806b2b7fa496d18d0fbbc Mon Sep 17 00:00:00 2001 From: gaurovgiri Date: Thu, 5 Oct 2023 14:21:54 +0545 Subject: [PATCH 04/10] fix: import name in testcase --- Data-Structures/Stack/test/EvaluateExpression.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structures/Stack/test/EvaluateExpression.test.js b/Data-Structures/Stack/test/EvaluateExpression.test.js index 55c8bf1747..4ad7789ae0 100644 --- a/Data-Structures/Stack/test/EvaluateExpression.test.js +++ b/Data-Structures/Stack/test/EvaluateExpression.test.js @@ -1,4 +1,4 @@ -import { evaluateExpression } from '../evaluateExpression.js'; +import { evaluateExpression } from '../EvaluateExpression.js'; describe('evaluateExpression', () => { it('should evaluate a valid expression', () => { From 23034dace59b7456afae2c60f42b11d35c1e6c79 Mon Sep 17 00:00:00 2001 From: gaurovgiri Date: Thu, 5 Oct 2023 14:58:52 +0545 Subject: [PATCH 05/10] fix: test case result --- Data-Structures/Stack/test/EvaluateExpression.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data-Structures/Stack/test/EvaluateExpression.test.js b/Data-Structures/Stack/test/EvaluateExpression.test.js index 4ad7789ae0..8067170356 100644 --- a/Data-Structures/Stack/test/EvaluateExpression.test.js +++ b/Data-Structures/Stack/test/EvaluateExpression.test.js @@ -2,9 +2,9 @@ import { evaluateExpression } from '../EvaluateExpression.js'; describe('evaluateExpression', () => { it('should evaluate a valid expression', () => { - const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 7 + const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 11 const result = evaluateExpression(expression); - expect(result).toBe(7); + expect(result).toBe(11); }); it('should handle division by zero', () => { From 409fbecfae9f6267af0d9fb218e448f6be2f4a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Oct 2023 16:03:14 +0200 Subject: [PATCH 06/10] Make clear that this is postfix --- Data-Structures/Stack/EvaluateExpression.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Data-Structures/Stack/EvaluateExpression.js b/Data-Structures/Stack/EvaluateExpression.js index c7e3b6600f..d26d494520 100644 --- a/Data-Structures/Stack/EvaluateExpression.js +++ b/Data-Structures/Stack/EvaluateExpression.js @@ -1,12 +1,12 @@ /** - * Evaluate a numeric operations string using a stack. + * 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 evaluateExpression(expression) { +function evaluatePostfixExpression(expression) { const stack = []; // Helper function to perform an operation and push the result to the stack @@ -62,5 +62,5 @@ function evaluateExpression(expression) { } } - export { evaluateExpression }; - \ No newline at end of file + export { evaluatePostfixExpression }; + From 5105fe989a56da099e4fc203161f603d9c7517a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Oct 2023 16:03:52 +0200 Subject: [PATCH 07/10] Update tests --- Data-Structures/Stack/test/EvaluateExpression.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Data-Structures/Stack/test/EvaluateExpression.test.js b/Data-Structures/Stack/test/EvaluateExpression.test.js index 8067170356..eea764cac2 100644 --- a/Data-Structures/Stack/test/EvaluateExpression.test.js +++ b/Data-Structures/Stack/test/EvaluateExpression.test.js @@ -1,21 +1,21 @@ -import { evaluateExpression } from '../EvaluateExpression.js'; +import { evaluatePostfixExpression } from '../EvaluateExpression.js'; -describe('evaluateExpression', () => { +describe('evaluatePostfixExpression', () => { it('should evaluate a valid expression', () => { const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 11 - const result = evaluateExpression(expression); + const result = evaluatePostfixExpression(expression); expect(result).toBe(11); }); it('should handle division by zero', () => { const expression = '3 0 /'; // Division by zero - const result = evaluateExpression(expression); + const result = evaluatePostfixExpression(expression); expect(result).toBe(null); }); it('should handle an invalid expression', () => { const expression = '3 * 4 2 / +'; // Invalid expression - const result = evaluateExpression(expression); + const result = evaluatePostfixExpression(expression); expect(result).toBe(null); }); From 4fd461f2d759c6e3b178feee1d2a259ad3e40eac Mon Sep 17 00:00:00 2001 From: gaurovgiri Date: Sun, 8 Oct 2023 16:06:32 +0545 Subject: [PATCH 08/10] add: see reference --- Data-Structures/Stack/EvaluateExpression.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Data-Structures/Stack/EvaluateExpression.js b/Data-Structures/Stack/EvaluateExpression.js index d26d494520..d96cff54da 100644 --- a/Data-Structures/Stack/EvaluateExpression.js +++ b/Data-Structures/Stack/EvaluateExpression.js @@ -1,8 +1,7 @@ /** * 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/ - * + * @see 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. */ From 9c790bc93d7f7e96b776d6be965f262be80c94e1 Mon Sep 17 00:00:00 2001 From: gaurovgiri Date: Sun, 8 Oct 2023 20:57:10 +0545 Subject: [PATCH 09/10] fixes mentioned issues --- Data-Structures/Stack/EvaluateExpression.js | 98 ++++++++++----------- 1 file changed, 46 insertions(+), 52 deletions(-) diff --git a/Data-Structures/Stack/EvaluateExpression.js b/Data-Structures/Stack/EvaluateExpression.js index d96cff54da..b776d4806a 100644 --- a/Data-Structures/Stack/EvaluateExpression.js +++ b/Data-Structures/Stack/EvaluateExpression.js @@ -2,64 +2,58 @@ * Evaluate a numeric operations string in postfix notation using a stack. * Supports basic arithmetic operations: +, -, *, / * @see https://www.geeksforgeeks.org/evaluation-of-postfix-expression/ - * @param {string} expression - Numeric operations expression to evaluate. + * @param {string} expression - Numeric operations expression to evaluate. Must be a valid postfix expression. * @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 stack = []; + + // Helper function to perform an operation and push the result to the stack + function performOperation(operator) { + const rightOp = stack.pop(); // Right operand is the top of the stack + const leftOp = stack.pop(); // Left operand is the next item on the stack + + if (leftOp === undefined || rightOp === undefined) { + return false; // Invalid expression } - - 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 + switch (operator) { + case '+': + stack.push(leftOp + rightOp); + break; + case '-': + stack.push(leftOp - rightOp); + break; + case '*': + stack.push(leftOp * rightOp); + break; + case '/': + if (rightOp === 0) { + return false; } - } + stack.push(leftOp / rightOp); + break; + default: + break; } - - if (stack.length === 1) { - // The final result should be on the stack - return stack[0]; + + } + + const tokens = expression.split(/\s+/); + + 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 { - return null; // Invalid expression + // If the token is an operator, perform the operation + const result = performOperation(token); + if (result === false) { + return null; // Invalid expression + } } } - - export { evaluatePostfixExpression }; - + + return (stack.length === 1) ? stack[0] : null; +} + +export { evaluatePostfixExpression }; From f0ee63cf3026245911f7cfcb01fba7cb22e60301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:31:14 +0200 Subject: [PATCH 10/10] Fix `default` case --- Data-Structures/Stack/EvaluateExpression.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Data-Structures/Stack/EvaluateExpression.js b/Data-Structures/Stack/EvaluateExpression.js index b776d4806a..e59a4a37c0 100644 --- a/Data-Structures/Stack/EvaluateExpression.js +++ b/Data-Structures/Stack/EvaluateExpression.js @@ -8,7 +8,7 @@ function evaluatePostfixExpression(expression) { const stack = []; - // Helper function to perform an operation and push the result to the stack + // Helper function to perform an operation and push the result to the stack. Returns success. function performOperation(operator) { const rightOp = stack.pop(); // Right operand is the top of the stack const leftOp = stack.pop(); // Left operand is the next item on the stack @@ -33,9 +33,9 @@ function evaluatePostfixExpression(expression) { stack.push(leftOp / rightOp); break; default: - break; + return false; // Unknown operator } - + return true; } const tokens = expression.split(/\s+/); @@ -46,8 +46,7 @@ function evaluatePostfixExpression(expression) { stack.push(parseFloat(token)); } else { // If the token is an operator, perform the operation - const result = performOperation(token); - if (result === false) { + if (!performOperation(token)) { return null; // Invalid expression } }