Exercise 1 solution def rpn(expr): st = [] for tok in expr.split(): if tok in ("+", "-", "*"): b = st.pop() # right operand comes off first a = st.pop() if tok == "+": st.append(a + b) elif tok == "-": st.append(a - b) else: st.append(a * b) else: st.append(int(tok)) return st[0] print(rpn("3 4 + 2 *"), rpn("5 1 2 + 4 * + 3 -")) Output: 14 14 The first value popped is the right-hand operand, so for "-" the order a - b matters. Swapping them would give wrong answers for subtraction.