Skip to content

Commit 0984fe0

Browse files
realDuYuanChaogithub-actions
authored andcommitted
Update infix to postfix (TheAlgorithms#3817)
* add test to infix_to_postfix_conversion * fixed pre-commit error * fixed build error * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 8582d89 commit 0984fe0

File tree

3 files changed

+45
-52
lines changed

3 files changed

+45
-52
lines changed

Diff for: DIRECTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@
674674
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_056/sol1.py)
675675
* Problem 057
676676
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_057/sol1.py)
677+
* Problem 058
678+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_058/sol1.py)
677679
* Problem 062
678680
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_062/sol1.py)
679681
* Problem 063
@@ -699,6 +701,8 @@
699701
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_080/sol1.py)
700702
* Problem 081
701703
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_081/sol1.py)
704+
* Problem 087
705+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_087/sol1.py)
702706
* Problem 091
703707
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_091/sol1.py)
704708
* Problem 097
@@ -817,6 +821,7 @@
817821
* [Prefix Function](https://github.com/TheAlgorithms/Python/blob/master/strings/prefix_function.py)
818822
* [Rabin Karp](https://github.com/TheAlgorithms/Python/blob/master/strings/rabin_karp.py)
819823
* [Remove Duplicate](https://github.com/TheAlgorithms/Python/blob/master/strings/remove_duplicate.py)
824+
* [Reverse Letters](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_letters.py)
820825
* [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py)
821826
* [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py)
822827
* [Swap Case](https://github.com/TheAlgorithms/Python/blob/master/strings/swap_case.py)

Diff for: data_structures/stacks/__init__.py

-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +0,0 @@
1-
class Stack:
2-
def __init__(self):
3-
self.stack = []
4-
self.top = 0
5-
6-
def is_empty(self):
7-
return self.top == 0
8-
9-
def push(self, item):
10-
if self.top < len(self.stack):
11-
self.stack[self.top] = item
12-
else:
13-
self.stack.append(item)
14-
15-
self.top += 1
16-
17-
def pop(self):
18-
if self.is_empty():
19-
return None
20-
else:
21-
self.top -= 1
22-
return self.stack[self.top]

Diff for: data_structures/stacks/infix_to_postfix_conversion.py

+40-30
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,67 @@
1-
import string
1+
"""
2+
https://en.wikipedia.org/wiki/Infix_notation
3+
https://en.wikipedia.org/wiki/Reverse_Polish_notation
4+
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
5+
"""
26

7+
from .balanced_parentheses import balanced_parentheses
38
from .stack import Stack
49

5-
__author__ = "Omkar Pathak"
610

7-
8-
def is_operand(char):
9-
return char in string.ascii_letters or char in string.digits
10-
11-
12-
def precedence(char):
13-
"""Return integer value representing an operator's precedence, or
11+
def precedence(char: str) -> int:
12+
"""
13+
Return integer value representing an operator's precedence, or
1414
order of operation.
15-
1615
https://en.wikipedia.org/wiki/Order_of_operations
1716
"""
18-
dictionary = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}
19-
return dictionary.get(char, -1)
20-
17+
return {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}.get(char, -1)
2118

22-
def infix_to_postfix(expression):
23-
"""Convert infix notation to postfix notation using the Shunting-yard
24-
algorithm.
2519

26-
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
27-
https://en.wikipedia.org/wiki/Infix_notation
28-
https://en.wikipedia.org/wiki/Reverse_Polish_notation
20+
def infix_to_postfix(expression_str: str) -> str:
21+
"""
22+
>>> infix_to_postfix("(1*(2+3)+4))")
23+
Traceback (most recent call last):
24+
...
25+
ValueError: Mismatched parentheses
26+
>>> infix_to_postfix("")
27+
''
28+
>>> infix_to_postfix("3+2")
29+
'3 2 +'
30+
>>> infix_to_postfix("(3+4)*5-6")
31+
'3 4 + 5 * 6 -'
32+
>>> infix_to_postfix("(1+2)*3/4-5")
33+
'1 2 + 3 * 4 / 5 -'
34+
>>> infix_to_postfix("a+b*c+(d*e+f)*g")
35+
'a b c * + d e * f + g * +'
36+
>>> infix_to_postfix("x^y/(5*z)+2")
37+
'x y ^ 5 z * / 2 +'
2938
"""
30-
stack = Stack(len(expression))
39+
if not balanced_parentheses(expression_str):
40+
raise ValueError("Mismatched parentheses")
41+
stack = Stack()
3142
postfix = []
32-
for char in expression:
33-
if is_operand(char):
43+
for char in expression_str:
44+
if char.isalpha() or char.isdigit():
3445
postfix.append(char)
35-
elif char not in {"(", ")"}:
36-
while not stack.is_empty() and precedence(char) <= precedence(stack.peek()):
37-
postfix.append(stack.pop())
38-
stack.push(char)
3946
elif char == "(":
4047
stack.push(char)
4148
elif char == ")":
4249
while not stack.is_empty() and stack.peek() != "(":
4350
postfix.append(stack.pop())
44-
# Pop '(' from stack. If there is no '(', there is a mismatched
45-
# parentheses.
46-
if stack.peek() != "(":
47-
raise ValueError("Mismatched parentheses")
4851
stack.pop()
52+
else:
53+
while not stack.is_empty() and precedence(char) <= precedence(stack.peek()):
54+
postfix.append(stack.pop())
55+
stack.push(char)
4956
while not stack.is_empty():
5057
postfix.append(stack.pop())
5158
return " ".join(postfix)
5259

5360

5461
if __name__ == "__main__":
62+
from doctest import testmod
63+
64+
testmod()
5565
expression = "a+b*(c^d-e)^(f+g*h)-i"
5666

5767
print("Infix to Postfix Notation demonstration:\n")

0 commit comments

Comments
 (0)