Log inBook a Demo
BuildSeniorBackendAuto-graded

Evaluate arithmetic with correct precedence and parentheses

Write your own parser for + - * /, unary minus and parentheses. Using eval() or a math library isn't allowed.evaluate(expr) should handle + - * /, unary minus, nested parentheses and decimals. * and / bind tighter than + and -, and operators of the same rank apply left to right. Today 2 + 3 * 4 returns 20, (2 + 3) * 4 crashes and 2 + returns a number. Malformed input and division by zero must raise EvalError, and no other exception should escape. Parse the expression yourself: eval, exec, compile and expression libraries are not allowed. SPEC.md has the full grammar.
45 min
python

You're done when

  • Ordinary expressions give the right value, including decimals and any amount of spacing.
  • * and / bind tighter than + and -, and parentheses override that.
  • Unary minus works at the start of an expression, after an operator and in front of a parenthesised group.
  • Malformed input such as 2 +, (1 + 2 or an empty string raises EvalError.
  • Division by zero raises EvalError, including through a sub-expression like 5 / (2 - 2).

What it measures

Parser designHow the parser encodes precedence and left-to-right grouping.
Edge casesWhether unary minus, nested parentheses and every error case are covered.
Own implementationWhether the evaluation is written by hand, without `eval` or an expression library.

Your environment

Terminal + editorA full shell in the browser
Coding agentAlready signed in, no API key to set up
Isolated workspaceOnly yours, wiped when you finish

Start this lab

45 min
Start labYou'll need to sign in to Anthropos first. The lab opens in a private cloud workspace with your coding agent ready to use.
AI Labs is part of Anthropos Workforce. Not on your plan yet? Book a demo