Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Evaluate Postfix With Bindings
00:00
5 left

Evaluate Postfix With Bindings

EasyPython

Problem

Tessian policy evaluation can represent arithmetic conditions in postfix notation so they can be processed efficiently. Given expression tokens and a mapping of variable names to integer values, evaluate the expression using the current bindings.

Tokens may be integer literals, variable names, or binary operators +, -, *, and /. Operators consume the two most recent values from the stack. For a / b, division must truncate toward zero. Every expression is valid, every referenced variable has a binding, and division by zero does not occur.

Formal Specification

Implement eval_postfix(tokens, bindings).

  • tokens is a list of strings in postfix order.
  • bindings is a dictionary mapping variable-name strings to integers.
  • Return the resulting integer.
  • Variable names contain lowercase English letters and underscores and do not begin with a digit.
  • A token consisting of an optional leading minus sign followed by digits is an integer literal.

Constraints

  • 1 <= len(tokens) <= 10^4
  • Each integer literal and binding value is between -10^9 and 10^9
  • The expression contains at least one value and has valid postfix syntax
  • At most len(tokens) distinct variable names are referenced
  • Division by zero does not occur

Function Signature

def eval_postfix(tokens, bindings):
Interviewer

Your question is Evaluate Postfix With Bindings. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.