Untitled

 avatar
unknown
plain_text
2 years ago
520 B
5
Indexable
s = input().split()
stack = []
res = []
ops = "^ */ +-"

for elem in s:
    if elem.isdigit():
        res.append(elem)
    elif elem == "(":
        stack.append(elem)
    elif elem == ")":
        while stack[-1] != "(":
            res.append(stack.pop())
        stack.pop()
    else:
        high_prior = ops[:ops.find(elem) + 2]
        while len(stack) > 0 and stack[-1] in high_prior:
            res.append(stack.pop())
        stack.append(elem)

while len(stack) > 0:
    res.append(stack.pop())

print(*res)
Editor is loading...