Created
December 9, 2022 11:28
-
-
Save modos/ed36d0fdb68617a45fb3919f11c8d159 to your computer and use it in GitHub Desktop.
شنگول و منگول
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Operators = set(['+', '-', '*', '/', '(', ')', '^']) # collection of Operators | |
Priority = {'+':1, '-':1, '*':2, '/':2, '^':3} # dictionary having priorities of Operators | |
def infixToPostfix(expression): | |
stack = [] # initialization of empty stack | |
output = '' | |
for character in expression: | |
if character not in Operators: # if an operand append in postfix expression | |
output+= character | |
elif character=='(': # else Operators push onto stack | |
stack.append('(') | |
elif character==')': | |
while stack and stack[-1]!= '(': | |
output+=stack.pop() | |
stack.pop() | |
else: | |
while stack and stack[-1]!='(' and Priority[character]<=Priority[stack[-1]]: | |
output+=stack.pop() | |
stack.append(character) | |
while stack: | |
output+=stack.pop() | |
return output | |
expression = input() | |
print(infixToPostfix(expression)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment