← Stack Discipline

Micro-Drill #134 — Eval RPN with stack

Stack Discipline Target: 10s

Stack-based evaluation pops two operands per operator. Handles all four arithmetic ops.

st = []
for t in tokens:
    if t in '+-*/':
        b, a = st.pop(), st.pop()
        st.append(int(eval(f'{a}{t}{b}')))
    else:
        st.append(int(t))

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #133 Micro #135 →