Answer the question
In order to leave comments, you need to log in
Convert from reverse Polish notation to regular notation. How?
At the input - the reverse Polish notation of the formula (on the stack). You need to convert it to normal.
Example:
Получаем:
2 3 + 6 2 / 5 2 * - +
Отдаем:
2+3+6/2-5*2
Answer the question
In order to leave comments, you need to log in
stack = "2 3 + 6 2 / 5 2 * - +".split()
def f():
op = stack.pop()
if op.isdigit():
return float(op)
else:
y = f()
x = f()
if op == "+":
return x + y
if op == "-":
return x - y
if op == "*":
return x * y
if op == "/":
return x / y
print(f())
stack = "2 3 + 6 2 / 5 2 * - +".split()
def g():
op = stack.pop()
if op.isdigit():
return op
else:
y = g()
x = g()
return ''.join(("(", x, op, y, ")")) # *
print(g())
https://learnc.info/adt/binary_tree_traversal.html
This can be described as a binary tree traversal, in its original form: post-order (left subtree, right subtree, operation), in the final form: in-order (left subtree, operation, right subtree).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question