Apply a flash sale price cap to a cart
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
capbecomes exactlycap. - A price at or below
capstays as it is. - You may change
pricesin place and return it, or return a new list.
Examples
cap_prices(prices: list[int], cap: int) -> list[int]Example 1
Input
cap_prices([12, 5, 30], 10)Output
[10, 5, 10]Example 2
Input
cap_prices([999, 1500, 250], 1000)Output
[999, 1000, 250]Example 3
Input
cap_prices([400, 400], 0)Output
[0, 0]