InferenceAI

Enter at least two characters to search.

Start free

Postfix Calculator

mediumStack25 min25 XP

Evaluate a list of tokens in reverse Polish (postfix) notation. Tokens are integers or one of +, -, *, /. Division truncates toward zero (so −7/2=−3-7 / 2 = -3).

The expression is always valid and never divides by zero.

Examples

EXAMPLE WALKTHROUGH(2 + 1) × 3

A guided view of the published example, not a trace of your code.

INPUT[["2","1","+","3","*"]]
TASKpostfix_calculator()
EXPECTED OUTPUT9

Start with the input values shown in this example.

Example 1 of 3

Input: tokens = ["2","1","+","3","*"]

Output: 9

Explanation: (2 + 1) × 3

Input: tokens = ["4","13","5","/","+"]

Output: 6

Explanation: 4 + 13 / 5

Input: tokens = ["-7","2","/"]

Output: -3

Explanation: truncation toward zero

Hint 1

Push numbers; on an operator, pop two values (right operand first).

Hint 2

Use int(a / b) to truncate toward zero.

Topics

DSAStackPython