Advertisement
Pit_Anonim

Untitled

Dec 10th, 2021
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 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. break
  24. queue.append(int(num))
  25. else:
  26. if (ls[s[i]] > 1):
  27. if (len(queue) == 0 or ls[s[i]] > ls[stack[-1]]):
  28. stack.append(s[i])
  29. else:
  30. while (len(stack)>0 and ls[s[i]] <= ls[stack[-1]]):
  31. queue.append(stack[-1])
  32. stack.pop()
  33.  
  34. stack.append(s[i])
  35.  
  36. else:
  37. if (s[i] == '('):
  38. stack.append(s[i])
  39. elif (s[i] == ')'):
  40. while(stack[-1] != '('):
  41. queue.append(stack[-1])
  42. stack.pop()
  43. stack.pop()
  44.  
  45. i = i + 1
  46.  
  47. while (len(stack) > 0):
  48. queue.append([stack[-1]])
  49. stack.pop()
  50.  
  51. print(queue)
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement