Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ls = {
- '+':3,
- '-':3,
- '*':4,
- '/':4,
- '(':1,
- ')':1
- }
- stack = []
- queue = []
- s = input()
- i = 0
- while(i < len(s)):
- if (s[i].isdigit()):
- num = '';
- while (True):
- if (i < len(s) and s[i].isdigit()):
- num = num + s[i]
- i = i + 1
- else:
- break
- queue.append(int(num))
- else:
- if (ls[s[i]] > 1):
- if (len(queue) == 0 or ls[s[i]] > ls[stack[-1]]):
- stack.append(s[i])
- else:
- while (len(stack)>0 and ls[s[i]] <= ls[stack[-1]]):
- queue.append(stack[-1])
- stack.pop()
- stack.append(s[i])
- else:
- if (s[i] == '('):
- stack.append(s[i])
- elif (s[i] == ')'):
- while(stack[-1] != '('):
- queue.append(stack[-1])
- stack.pop()
- stack.pop()
- i = i + 1
- while (len(stack) > 0):
- queue.append([stack[-1]])
- stack.pop()
- print(queue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement