2019-03-01 16:53:29 +00:00
|
|
|
"""
|
|
|
|
Output:
|
|
|
|
|
|
|
|
Enter an Infix Equation = a + b ^c
|
|
|
|
Symbol | Stack | Postfix
|
|
|
|
----------------------------
|
|
|
|
c | | c
|
|
|
|
^ | ^ | c
|
|
|
|
b | ^ | cb
|
|
|
|
+ | + | cb^
|
|
|
|
a | + | cb^a
|
|
|
|
| | cb^a+
|
|
|
|
|
2019-11-16 07:05:00 +00:00
|
|
|
a+b^c (Infix) -> +a^bc (Prefix)
|
2019-03-01 16:53:29 +00:00
|
|
|
"""
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2023-10-11 09:00:49 +00:00
|
|
|
def infix_2_postfix(infix: str) -> str:
|
2023-10-10 16:52:53 +00:00
|
|
|
"""
|
|
|
|
>>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
|
|
|
|
Symbol | Stack | Postfix
|
|
|
|
----------------------------
|
|
|
|
a | | a
|
|
|
|
+ | + | a
|
|
|
|
b | + | ab
|
|
|
|
^ | +^ | ab
|
|
|
|
c | +^ | abc
|
|
|
|
| + | abc^
|
|
|
|
| | abc^+
|
|
|
|
'abc^+'
|
2023-10-11 09:00:49 +00:00
|
|
|
|
|
|
|
>>> infix_2_postfix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE
|
|
|
|
Symbol | Stack | Postfix
|
|
|
|
-------------------------------------------
|
|
|
|
1 | | 1
|
|
|
|
* | * | 1
|
|
|
|
( | *( | 1
|
|
|
|
( | *(( | 1
|
|
|
|
- | *((- | 1
|
|
|
|
a | *((- | 1a
|
|
|
|
) | *( | 1a-
|
|
|
|
* | *(* | 1a-
|
|
|
|
2 | *(* | 1a-2
|
|
|
|
+ | *(+ | 1a-2*
|
|
|
|
b | *(+ | 1a-2*b
|
|
|
|
) | * | 1a-2*b+
|
|
|
|
| | 1a-2*b+*
|
|
|
|
'1a-2*b+*'
|
|
|
|
|
2023-10-10 16:52:53 +00:00
|
|
|
>>> infix_2_postfix("")
|
|
|
|
Symbol | Stack | Postfix
|
|
|
|
----------------------------
|
|
|
|
''
|
2023-10-11 09:00:49 +00:00
|
|
|
|
|
|
|
>>> infix_2_postfix("(()")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: invalid expression
|
|
|
|
|
2023-10-10 16:52:53 +00:00
|
|
|
>>> infix_2_postfix("())")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
IndexError: list index out of range
|
|
|
|
"""
|
2022-10-12 22:54:20 +00:00
|
|
|
stack = []
|
|
|
|
post_fix = []
|
2019-10-05 05:14:13 +00:00
|
|
|
priority = {
|
|
|
|
"^": 3,
|
|
|
|
"*": 2,
|
|
|
|
"/": 2,
|
|
|
|
"%": 2,
|
|
|
|
"+": 1,
|
|
|
|
"-": 1,
|
|
|
|
} # Priority of each operator
|
2023-10-11 09:00:49 +00:00
|
|
|
print_width = max(len(infix), 7)
|
2019-03-01 16:53:29 +00:00
|
|
|
|
|
|
|
# Print table header for output
|
2019-10-05 05:14:13 +00:00
|
|
|
print(
|
|
|
|
"Symbol".center(8),
|
|
|
|
"Stack".center(print_width),
|
|
|
|
"Postfix".center(print_width),
|
|
|
|
sep=" | ",
|
|
|
|
)
|
|
|
|
print("-" * (print_width * 3 + 7))
|
2019-03-01 16:53:29 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
for x in infix:
|
2019-10-05 05:14:13 +00:00
|
|
|
if x.isalpha() or x.isdigit():
|
2022-10-12 22:54:20 +00:00
|
|
|
post_fix.append(x) # if x is Alphabet / Digit, add it to Postfix
|
2019-10-05 05:14:13 +00:00
|
|
|
elif x == "(":
|
2022-10-12 22:54:20 +00:00
|
|
|
stack.append(x) # if x is "(" push to Stack
|
2019-10-05 05:14:13 +00:00
|
|
|
elif x == ")": # if x is ")" pop stack until "(" is encountered
|
2023-10-11 09:00:49 +00:00
|
|
|
if len(stack) == 0: # close bracket without open bracket
|
|
|
|
raise IndexError("list index out of range")
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
while stack[-1] != "(":
|
|
|
|
post_fix.append(stack.pop()) # Pop stack & add the content to Postfix
|
|
|
|
stack.pop()
|
2024-03-28 17:25:41 +00:00
|
|
|
elif len(stack) == 0:
|
|
|
|
stack.append(x) # If stack is empty, push x to stack
|
|
|
|
else: # while priority of x is not > priority of element in the stack
|
|
|
|
while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]:
|
|
|
|
post_fix.append(stack.pop()) # pop stack & add to Postfix
|
|
|
|
stack.append(x) # push x to stack
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
print(
|
|
|
|
x.center(8),
|
2022-10-12 22:54:20 +00:00
|
|
|
("".join(stack)).ljust(print_width),
|
|
|
|
("".join(post_fix)).ljust(print_width),
|
2019-10-05 05:14:13 +00:00
|
|
|
sep=" | ",
|
|
|
|
) # Output in tabular format
|
2019-03-01 16:53:29 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
while len(stack) > 0: # while stack is not empty
|
2023-10-11 09:00:49 +00:00
|
|
|
if stack[-1] == "(": # open bracket with no close bracket
|
|
|
|
raise ValueError("invalid expression")
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
post_fix.append(stack.pop()) # pop stack & add to Postfix
|
2019-10-05 05:14:13 +00:00
|
|
|
print(
|
|
|
|
" ".center(8),
|
2022-10-12 22:54:20 +00:00
|
|
|
("".join(stack)).ljust(print_width),
|
|
|
|
("".join(post_fix)).ljust(print_width),
|
2019-10-05 05:14:13 +00:00
|
|
|
sep=" | ",
|
|
|
|
) # Output in tabular format
|
2019-03-01 16:53:29 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
return "".join(post_fix) # return Postfix as str
|
2019-03-01 16:53:29 +00:00
|
|
|
|
|
|
|
|
2023-10-11 09:00:49 +00:00
|
|
|
def infix_2_prefix(infix: str) -> str:
|
2023-10-10 16:52:53 +00:00
|
|
|
"""
|
|
|
|
>>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
|
|
|
|
Symbol | Stack | Postfix
|
|
|
|
----------------------------
|
|
|
|
c | | c
|
|
|
|
^ | ^ | c
|
|
|
|
b | ^ | cb
|
|
|
|
+ | + | cb^
|
|
|
|
a | + | cb^a
|
|
|
|
| | cb^a+
|
|
|
|
'+a^bc'
|
|
|
|
|
2023-10-11 09:00:49 +00:00
|
|
|
>>> infix_2_prefix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE
|
|
|
|
Symbol | Stack | Postfix
|
|
|
|
-------------------------------------------
|
|
|
|
( | ( |
|
|
|
|
b | ( | b
|
|
|
|
+ | (+ | b
|
|
|
|
2 | (+ | b2
|
|
|
|
* | (+* | b2
|
|
|
|
( | (+*( | b2
|
|
|
|
a | (+*( | b2a
|
|
|
|
- | (+*(- | b2a
|
|
|
|
) | (+* | b2a-
|
|
|
|
) | | b2a-*+
|
|
|
|
* | * | b2a-*+
|
|
|
|
1 | * | b2a-*+1
|
|
|
|
| | b2a-*+1*
|
|
|
|
'*1+*-a2b'
|
2023-10-10 16:52:53 +00:00
|
|
|
|
|
|
|
>>> infix_2_prefix('')
|
|
|
|
Symbol | Stack | Postfix
|
|
|
|
----------------------------
|
|
|
|
''
|
|
|
|
|
|
|
|
>>> infix_2_prefix('(()')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
IndexError: list index out of range
|
|
|
|
|
2023-10-11 09:00:49 +00:00
|
|
|
>>> infix_2_prefix('())')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: invalid expression
|
2023-10-10 16:52:53 +00:00
|
|
|
"""
|
2023-10-11 09:00:49 +00:00
|
|
|
reversed_infix = list(infix[::-1]) # reverse the infix equation
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2023-10-11 09:00:49 +00:00
|
|
|
for i in range(len(reversed_infix)):
|
|
|
|
if reversed_infix[i] == "(":
|
|
|
|
reversed_infix[i] = ")" # change "(" to ")"
|
|
|
|
elif reversed_infix[i] == ")":
|
|
|
|
reversed_infix[i] = "(" # change ")" to "("
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2023-10-11 09:00:49 +00:00
|
|
|
# call infix_2_postfix on Infix, return reverse of Postfix
|
|
|
|
return (infix_2_postfix("".join(reversed_infix)))[::-1]
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-03-01 16:53:29 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-10-10 16:52:53 +00:00
|
|
|
from doctest import testmod
|
|
|
|
|
|
|
|
testmod()
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
Infix = input("\nEnter an Infix Equation = ") # Input an Infix equation
|
|
|
|
Infix = "".join(Infix.split()) # Remove spaces from the input
|
2019-03-01 16:53:29 +00:00
|
|
|
print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")
|