Untitled

mail@pastecode.io avatar
unknown
plain_text
10 months ago
520 B
2
Indexable
Never
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)