Advertisement
namemkazaza

Untitled

Nov 28th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. from collections import deque
  2.  
  3. input_string = input().rstrip()
  4.  
  5. new = ''
  6. for i in input_string:
  7. if i in "+-/*()":
  8. new += ' ' + i + ' '
  9. else:
  10. new += i
  11.  
  12. infix = list(new.split())
  13.  
  14. priority = {"sqrt":3, "sin":3,"cos":3, "abs":2, "*":2, "/":2, "+":1, "-":1}
  15.  
  16. stack = deque()
  17.  
  18. output = deque()
  19.  
  20. for j in infix:
  21. if j in priority.keys():
  22. if not stack or stack[-1] == '(':
  23. stack.append(j)
  24. elif priority[j] > priority[stack[-1]]:
  25. stack.append(j)
  26. else:
  27. while stack:
  28. if stack[-1] == '(':
  29. break
  30. elif priority[j] <= priority[stack[-1]]:
  31. output.append(stack.pop())
  32. else:
  33. break
  34. stack.append(j)
  35. elif j == '(':
  36. stack.append(j)
  37. elif j == ')':
  38. while stack[-1] != '(':
  39. output.append(stack.pop())
  40. stack.pop()
  41. else:
  42. output.append(j)
  43.  
  44. while stack:
  45. output.append(stack.pop())
  46. print(*output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement