Skip to content

Commit 72d19e8

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent fdca441 commit 72d19e8

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

Diff for: data_structures/stacks/infix_to_prefix_conversion.py

+35-31
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def infix_2_postfix(infix: str) -> str:
2626
"+": 1,
2727
"-": 1,
2828
} # Priority of each operator
29-
29+
3030
print_width = len(infix) if (len(infix) > 7) else 7
3131

3232
# Print table header for output
@@ -44,18 +44,21 @@ def infix_2_postfix(infix: str) -> str:
4444
elif x == "(":
4545
stack.append(x) # if x is "(" push to Stack
4646
elif x == ")": # if x is ")" pop stack until "(" is encountered
47-
if len(stack) == 0: # close bracket without open bracket
47+
if len(stack) == 0: # close bracket without open bracket
4848
raise ValueError("Invalid bracket position(s)")
49-
49+
5050
while stack[-1] != "(":
5151
post_fix.append(stack.pop()) # Pop stack & add the content to Postfix
5252
stack.pop()
5353
else:
5454
if len(stack) == 0:
5555
stack.append(x) # If stack is empty, push x to stack
5656
else: # while priority of x is not > priority of element in the stack
57-
while len(stack) > 0 and stack[-1] != '(' \
58-
and priority[x] <= priority[stack[-1]]:
57+
while (
58+
len(stack) > 0
59+
and stack[-1] != "("
60+
and priority[x] <= priority[stack[-1]]
61+
):
5962
post_fix.append(stack.pop()) # pop stack & add to Postfix
6063
stack.append(x) # push x to stack
6164

@@ -67,9 +70,9 @@ def infix_2_postfix(infix: str) -> str:
6770
) # Output in tabular format
6871

6972
while len(stack) > 0: # while stack is not empty
70-
if stack[-1] == '(': # open bracket with no close bracket
73+
if stack[-1] == "(": # open bracket with no close bracket
7174
raise ValueError("Invalid bracket position(s)")
72-
75+
7376
post_fix.append(stack.pop()) # pop stack & add to Postfix
7477
print(
7578
" ".center(8),
@@ -86,32 +89,32 @@ def infix_2_prefix(infix: str) -> str:
8689
>>> infix_2_prefix('a+b^c')
8790
Symbol | Stack | Postfix
8891
----------------------------
89-
c | | c
90-
^ | ^ | c
91-
b | ^ | cb
92-
+ | + | cb^
93-
a | + | cb^a
94-
| | cb^a+
92+
c | | c
93+
^ | ^ | c
94+
b | ^ | cb
95+
+ | + | cb^
96+
a | + | cb^a
97+
| | cb^a+
9598
'+a^bc'
96-
99+
97100
>>> infix_2_prefix('1*((-a)*2+b)')
98-
Symbol | Stack | Postfix
101+
Symbol | Stack | Postfix
99102
-------------------------------------------
100-
( | ( |
101-
b | ( | b
102-
+ | (+ | b
103-
2 | (+ | b2
104-
* | (+* | b2
105-
( | (+*( | b2
106-
a | (+*( | b2a
107-
- | (+*(- | b2a
108-
) | (+* | b2a-
109-
) | | b2a-*+
110-
* | * | b2a-*+
111-
1 | * | b2a-*+1
112-
| | b2a-*+1*
103+
( | ( |
104+
b | ( | b
105+
+ | (+ | b
106+
2 | (+ | b2
107+
* | (+* | b2
108+
( | (+*( | b2
109+
a | (+*( | b2a
110+
- | (+*(- | b2a
111+
) | (+* | b2a-
112+
) | | b2a-*+
113+
* | * | b2a-*+
114+
1 | * | b2a-*+1
115+
| | b2a-*+1*
113116
'*1+*-a2b'
114-
117+
115118
>>> infix_2_prefix('')
116119
Symbol | Stack | Postfix
117120
----------------------------
@@ -128,7 +131,7 @@ def infix_2_prefix(infix: str) -> str:
128131
ValueError: Invalid bracket position(s)
129132
"""
130133
infix = list(infix[::-1]) # reverse the infix equation
131-
134+
132135
for i in range(len(infix)):
133136
if infix[i] == "(":
134137
infix[i] = ")" # change "(" to ")"
@@ -142,8 +145,9 @@ def infix_2_prefix(infix: str) -> str:
142145

143146
if __name__ == "__main__":
144147
from doctest import testmod
148+
145149
testmod()
146-
150+
147151
Infix = input("\nEnter an Infix Equation = ") # Input an Infix equation
148152
Infix = "".join(Infix.split()) # Remove spaces from the input
149153
print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")

0 commit comments

Comments
 (0)