Operator Precedence Puzzle

Medium ⏱ 12 min 49% acceptance ★★★★☆ 4.2
Write a function evaluate_formula(a, b, c, d) that returns the result of the expression a + b * c 2 - d / 2</code>, applying Python's standard operator precedence (<code> highest, then *//, then +/-) without adding any explicit parentheses of your own to change the grouping.

Examples

Example 1
Input
evaluate_formula(1, 2, 3, 4)
Output
17.0
Explanation

1 + 2*9 - 4/2 = 1 + 18 - 2.0 = 17.0 (** binds before *, which binds before +/-).

Example 2
Input
evaluate_formula(0, 0, 0, 0)
Output
0.0
Explanation

All terms are zero.

Constraints

  • a, b, c, d are numbers.

Topics

Operatorsoperator precedence

Companies

AmazonOracle

Hints

Hint 1

Just write the expression literally: `a + b * c ** 2 - d / 2`; Python's precedence handles the rest.

Hint 2

Remember `/` always produces a float, which makes the whole result a float.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.