Apply a Discount Strategy Function

Medium ⏱ 12 min 53% acceptance ★★★★☆ 4.2
Write a function apply_discount(price, strategy) where strategy is itself a function that takes a price and returns a discounted price. apply_discount should simply call strategy(price) and return the result, rounded to 2 decimal places.

Examples

Example 1
Input
def ten_percent_off(p): return p * 0.9
apply_discount(200, ten_percent_off)
Output
180.0
Explanation

strategy(200) = 200 * 0.9 = 180.0.

Constraints

  • strategy is guaranteed to be a callable that accepts one numeric argument.
  • Result must be rounded to 2 decimal places.

Topics

Functionsfirst-class functions

Companies

RazorpayPayPal

Hints

Hint 1

Functions are values in Python — you can pass one as an ordinary argument.

Hint 2

Use round(value, 2) on the strategy result.

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