Skip to content

Instantly share code, notes, and snippets.

@modos
Created December 9, 2022 11:28
Show Gist options
  • Save modos/ed36d0fdb68617a45fb3919f11c8d159 to your computer and use it in GitHub Desktop.
Save modos/ed36d0fdb68617a45fb3919f11c8d159 to your computer and use it in GitHub Desktop.
شنگول و منگول
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