Advertisement
Pit_Anonim

Untitled

Dec 10th, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. ls = {
  2. '+':3,
  3. '-':3,
  4. '*':4,
  5. '/':4,
  6. '(':1,
  7. ')':1
  8. }
  9. stack = []
  10. queue = []
  11. s = input()
  12.  
  13. i = 0
  14. while(i < len(s)):
  15.  
  16. if (s[i].isdigit()):
  17. num = '';
  18. while (True):
  19. if (i < len(s) and s[i].isdigit()):
  20. num = num + s[i]
  21. i = i + 1
  22. else:
  23. i = i - 1
  24. break
  25. queue.append(int(num))
  26. else:
  27. if (ls[s[i]] > 1):
  28. if (len(queue) == 0 or (len(stack) > 0 and ls[s[i]] > ls[stack[-1]])):
  29. stack.append(s[i])
  30. else:
  31. while (len(stack)>0 and ls[s[i]] <= ls[stack[-1]]):
  32. queue.append(stack[-1])
  33. stack.pop()
  34.  
  35. stack.append(s[i])
  36.  
  37. else:
  38. if (s[i] == '('):
  39. stack.append(s[i])
  40. elif (s[i] == ')'):
  41. while(stack[-1] != '('):
  42. queue.append(stack[-1])
  43. stack.pop()
  44. stack.pop()
  45.  
  46. i = i + 1
  47.  
  48. while (len(stack) > 0):
  49. queue.append([stack[-1]])
  50. stack.pop()
  51.  
  52. print(queue)
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement