CJCoding With Joseph
15free / daySign inCreate account

Postfix Calculator With A Stack

The Stack class is given. Write evaluate_postfix(tokens: list[str]) -> int that evaluates an expression written in postfix notation, where each operator follows its two operands, so 3 4 + 2 * means (3 + 4) * 2. Walk the tokens: a number (test it with isdigit()) is converted with int() and pushed; an operator +, - or * pops the right operand first and the left operand second, applies the operation and pushes the result. The expression is always valid and only uses non-negative whole numbers; the answer is whatever is left on the stack at the end. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one line of space-separated tokens, which the tests split and pass to your function before printing the value it returns.

Expected Output:

14

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & Algorithms
📥 Auto-Input:
3 4 + 2 *\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.