Tiered Loyalty Discount Calculator

Medium ⏱ 12 min 58% acceptance ★★★★☆ 4.3
Write a function loyalty_price(price, tier="Bronze", *, extra_off=0) that applies a percentage discount based on tier ("Bronze": 0%, "Silver": 5%, "Gold": 10%, "Platinum": 15%), then subtracts an additional flat extra_off amount (keyword-only, defaulting to 0), and returns the final price rounded to 2 decimal places. The final price must never go below 0.

Examples

Example 1
Input
loyalty_price(1000, "Gold")
Output
900.0
Explanation

10% off 1000 is 900, no extra_off supplied.

Example 2
Input
loyalty_price(1000, "Silver", extra_off=100)
Output
850.0
Explanation

5% off 1000 = 950, then minus 100 extra_off = 850.0.

Example 3
Input
loyalty_price(50, "Bronze", extra_off=1000)
Output
0.0
Explanation

The subtraction would go negative, so it is clamped to 0.0.

Constraints

  • tier is one of Bronze, Silver, Gold, Platinum.
  • extra_off must be passed by keyword only.
  • Result is clamped at a minimum of 0.

Topics

Functionsdefault arguments

Companies

FlipkartMyntraAmazon

Hints

Hint 1

Use a dict mapping tier names to discount percentages.

Hint 2

Apply max(0, computed_price) before rounding, or round then clamp — either order works here since extra_off and rates are non-negative.

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