Skip to content

Commit d3199da

Browse files
echoajcclauss
andauthored
Created Dijkstra's Two Stack Algorithm (TheAlgorithms#2321)
* created dijkstra's two stack algorithm * Made changes to dijkstras two stack algorithm for documentation and testing purposes. * Made changes to dijkstras two stack algorithm for documentation and testing purposes. * Fixed Grammar Mistake * Added Explanation Reference * Imported stack instead of using my own Changed a few minor things. * Imported stack instead of using my own Changed a few minor things. * Update data_structures/stacks/dijkstras_two_stack_algorithm.py Co-authored-by: Christian Clauss <[email protected]> * Update dijkstras_two_stack_algorithm.py Co-authored-by: Christian Clauss <[email protected]>
1 parent 456893c commit d3199da

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
Author: Alexander Joslin
3+
GitHub: github.com/echoaj
4+
5+
Explanation: https://medium.com/@haleesammar/implemented-in-js-dijkstras-2-stack-
6+
algorithm-for-evaluating-mathematical-expressions-fc0837dae1ea
7+
8+
We can use Dijkstra's two stack algorithm to solve an equation
9+
such as: (5 + ((4 * 2) * (2 + 3)))
10+
11+
THESE ARE THE ALGORITHM'S RULES:
12+
RULE 1: Scan the expression from left to right. When an operand is encountered,
13+
push it onto the the operand stack.
14+
15+
RULE 2: When an operator is encountered in the expression,
16+
push it onto the operator stack.
17+
18+
RULE 3: When a left parenthesis is encountered in the expression, ignore it.
19+
20+
RULE 4: When a right parenthesis is encountered in the expression,
21+
pop an operator off the operator stack. The two operands it must
22+
operate on must be the last two operands pushed onto the operand stack.
23+
We therefore pop the operand stack twice, perform the operation,
24+
and push the result back onto the operand stack so it will be available
25+
for use as an operand of the next operator popped off the operator stack.
26+
27+
RULE 5: When the entire infix expression has been scanned, the value left on
28+
the operand stack represents the value of the expression.
29+
30+
NOTE: It only works with whole numbers.
31+
"""
32+
__author__ = "Alexander Joslin"
33+
34+
from .stack import Stack
35+
36+
import operator as op
37+
38+
39+
def dijkstras_two_stack_algorithm(equation: str) -> int:
40+
"""
41+
DocTests
42+
>>> dijkstras_two_stack_algorithm("(5 + 3)")
43+
8
44+
>>> dijkstras_two_stack_algorithm("((9 - (2 + 9)) + (8 - 1))")
45+
5
46+
>>> dijkstras_two_stack_algorithm("((((3 - 2) - (2 + 3)) + (2 - 4)) + 3)")
47+
-3
48+
49+
:param equation: a string
50+
:return: result: an integer
51+
"""
52+
operators = {"*": op.mul, "/": op.truediv, "+": op.add, "-": op.sub}
53+
54+
operand_stack = Stack()
55+
operator_stack = Stack()
56+
57+
for i in equation:
58+
if i.isdigit():
59+
# RULE 1
60+
operand_stack.push(int(i))
61+
elif i in operators:
62+
# RULE 2
63+
operator_stack.push(i)
64+
elif i == ")":
65+
# RULE 4
66+
opr = operator_stack.peek()
67+
operator_stack.pop()
68+
num1 = operand_stack.peek()
69+
operand_stack.pop()
70+
num2 = operand_stack.peek()
71+
operand_stack.pop()
72+
73+
total = operators[opr](num2, num1)
74+
operand_stack.push(total)
75+
76+
# RULE 5
77+
return operand_stack.peek()
78+
79+
80+
if __name__ == "__main__":
81+
equation = "(5 + ((4 * 2) * (2 + 3)))"
82+
# answer = 45
83+
print(f"{equation} = {dijkstras_two_stack_algorithm(equation)}")

0 commit comments

Comments
 (0)