forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostfixToPrefix.py
37 lines (28 loc) · 947 Bytes
/
PostfixToPrefix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop() if not self.empty() else None
def top(self):
return self.items[-1] if not self.empty() else None
def empty(self):
return len(self.items) == 0
def potpr(postfix):
s = Stack()
for t in postfix:
if t.isalnum(): # Check if the character is an operand
s.push(t)
else: # It's an operator
operand2 = s.pop()
operand1 = s.pop()
prefix_expr = f"{t}{operand1}{operand2}"
s.push(prefix_expr)
prefix = s.pop()
print("\nPostfix to Prefix expression is...")
print(prefix)
# Example usage:
if __name__ == "__main__":
postfix_expr = input("Enter postfix expression: ")
potpr(postfix_expr)