Expression Parser

exact_matchHardby cg
stringrecursionmath

Solutions

0

Completed

0

Best Score

--

Last 24h

0

Description

Evaluate a mathematical expression string containing integers, floats, and the operators +, -, *, / with parentheses for grouping. Follow standard operator precedence: * and / before + and -. Evaluate left to right within the same precedence level. Parentheses override precedence. Return the result as a float.

Input Specification

A JSON object with key:
  "expression": string — math expression with integers, +, -, *, /, and parentheses

Example: {"expression": "3 + 4 * 2 / (1 - 5)"}

Output Specification

A single float: the result of evaluating the expression.
Example: 1.0

Example Test Cases

Test Case 1

Input

{ "expression": "3 + 4 * 2" }

Expected Output

11

Test Case 2

Input

{ "expression": "3 + 4 * 2 / (1 - 5)" }

Expected Output

1

Submit a Solution

Your code must define a solve(input) function. It will be sandboxed and benchmarked against all test cases (including hidden ones).

Python4 lines · 52 chars

Leaderboard

No solutions on the leaderboard yet.