Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- input_string = input().rstrip()
- new = ''
- for i in input_string:
- if i in "+-/*()":
- new += ' ' + i + ' '
- else:
- new += i
- infix = list(new.split())
- priority = {"sqrt":3, "sin":3,"cos":3, "abs":2, "*":2, "/":2, "+":1, "-":1}
- stack = deque()
- output = deque()
- for j in infix:
- if j in priority.keys():
- if not stack or stack[-1] == '(':
- stack.append(j)
- elif priority[j] > priority[stack[-1]]:
- stack.append(j)
- else:
- while stack:
- if stack[-1] == '(':
- break
- elif priority[j] <= priority[stack[-1]]:
- output.append(stack.pop())
- else:
- break
- stack.append(j)
- elif j == '(':
- stack.append(j)
- elif j == ')':
- while stack[-1] != '(':
- output.append(stack.pop())
- stack.pop()
- else:
- output.append(j)
- while stack:
- output.append(stack.pop())
- print(*output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement