KatInterview

Parenthesized Integer Calculator with Unary Negation

Stacks & QueuesAuto-graded · 25 test cases
MediumAuto-gradedStacks & Queuesstackparsingexpression-evaluation

Last updated

Evaluate an integer expression with +, -, *, /, parentheses and unary minus in one iterative pass, using precedence rules and division that truncates toward zero.

Parenthesized Integer Calculator with Unary Negation

You are given a string expr that holds an arithmetic formula. Compute its value and return it as an integer.

The formula may contain:

  • non-negative integer literals (possibly several digits long),
  • the binary operators +, -, * and /,
  • opening and closing parentheses,
  • the minus sign used as a unary (prefix) operator, e.g. -(3 + 2), 2 * -3, or --4,
  • space characters, which carry no meaning and may appear anywhere between tokens (including between a unary minus and what follows it).

Evaluation rules

  1. Anything inside parentheses is evaluated before the surrounding operators are applied.
  2. * and / bind more tightly than binary + and -.
  3. Operators of equal rank are applied from left to right, so 8 - 3 - 2 is 3 and 8 / 2 / 2 is 2.
  4. A unary minus binds tighter than every binary operator: it negates the operand immediately to its right (a number, a parenthesized group, or another unary minus) before any binary operator uses that value.
  5. Division of integers truncates toward zero: -5 / 2 is -2 and 7 / -2 is -3. Division by zero never occurs.
  6. The input is always a well-formed expression. A unary plus never appears.

You must not use eval or any equivalent facility that evaluates source text. The nesting depth of parentheses may be very large (tens of thousands of levels), so the evaluation should be iterative rather than recursive.

Task

Implement evaluate_expression(expr) which returns the integer value of the expression.

Constraints

  • 1 <= len(expr) <= 10^5
  • expr consists only of digits, +, -, *, /, (, ) and spaces.
  • The expression is valid, and every intermediate and final value fits in a 32-bit signed integer.
  • Do not use eval, and avoid recursion.

Examples

Example 1

Input: evaluate_expression('3 + 2 * 2')

Output: 7

Example 2

Input: evaluate_expression('(1 + (4 + 5 + 2) - 3) + (6 + 8)')

Output: 23

Example 3

Input: evaluate_expression('- (3 + 2)')

Output: -5

Hints

Hint 1

Keep two stacks: one for numbers and one for operators (including opening parentheses). Scan the string once from left to right.

Hint 2

When a binary operator arrives, first apply any operators on top of the operator stack with precedence greater than or equal to it (stopping at an opening parenthesis), then push the new one. Using 'greater or equal' gives left-to-right evaluation.

Hint 3

Track whether the next token should be an operand. A '-' seen in that state is unary: push a special high-precedence negation operator that consumes one value. Also remember that Python's // floors, so divide absolute values and fix the sign.