data_structures.stacks.dijkstras_two_stack_algorithm¶
Author: Alexander Joslin GitHub: github.com/echoaj
- Explanation: https://medium.com/@haleesammar/implemented-in-js-dijkstras-2-stack-
algorithm-for-evaluating-mathematical-expressions-fc0837dae1ea
We can use Dijkstra’s two stack algorithm to solve an equation such as: (5 + ((4 * 2) * (2 + 3)))
THESE ARE THE ALGORITHM’S RULES: RULE 1: Scan the expression from left to right. When an operand is encountered,
push it onto the operand stack.
- RULE 2: When an operator is encountered in the expression,
push it onto the operator stack.
RULE 3: When a left parenthesis is encountered in the expression, ignore it.
- RULE 4: When a right parenthesis is encountered in the expression,
pop an operator off the operator stack. The two operands it must operate on must be the last two operands pushed onto the operand stack. We therefore pop the operand stack twice, perform the operation, and push the result back onto the operand stack so it will be available for use as an operand of the next operator popped off the operator stack.
- RULE 5: When the entire infix expression has been scanned, the value left on
the operand stack represents the value of the expression.
NOTE: It only works with whole numbers.
Attributes¶
Functions¶
DocTests |
Module Contents¶
- data_structures.stacks.dijkstras_two_stack_algorithm.dijkstras_two_stack_algorithm(equation: str) int ¶
DocTests >>> dijkstras_two_stack_algorithm(“(5 + 3)”) 8 >>> dijkstras_two_stack_algorithm(“((9 - (2 + 9)) + (8 - 1))”) 5 >>> dijkstras_two_stack_algorithm(“((((3 - 2) - (2 + 3)) + (2 - 4)) + 3)”) -3
- Parameters:
equation – a string
- Returns:
result: an integer
- data_structures.stacks.dijkstras_two_stack_algorithm.__author__ = 'Alexander Joslin'¶
- data_structures.stacks.dijkstras_two_stack_algorithm.equation = '(5 + ((4 * 2) * (2 + 3)))'¶