Apply a flash sale price cap to a cart

Write codeEasy10 min

Your task

During a flash sale, no item in a cart can cost more than a set maximum. The checkout applies that cap to each item before it adds up the total.

Write cap_prices(prices, cap) that returns the cart's prices after the cap.

Input and output:

  • prices: a list of up to 100,000 prices in cents, each an integer between 0 and 1,000,000. It may be empty.
  • cap: the maximum price in cents, an integer between 0 and 1,000,000.
  • Returns a list of integers, one per item, in the same order as prices.

Rules:

  • A price above cap becomes exactly cap.
  • A price at or below cap stays as it is.
  • You may change prices in place and return it, or return a new list.

Examples

cap_prices(prices: list[int], cap: int) -> list[int]
Example 1
Inputcap_prices([12, 5, 30], 10)
Output[10, 5, 10]
Example 2
Inputcap_prices([999, 1500, 250], 1000)
Output[999, 1000, 250]
Example 3
Inputcap_prices([400, 400], 0)
Output[0, 0]