The Third-Cheapest Price

Python for AIMedium+10 XP+10

The Third-Cheapest Price

A price-comparison site has a list of prices for one product. The same price often appears more than once.

Task: write kth_cheapest(prices, k). Return the k-th cheapest different price.

  • k = 1 means the cheapest, k = 2 the next cheapest, and so on.
  • Repeated prices only count once: in [3, 3, 5], the 2nd cheapest is 5.
  • If there are fewer than k different prices, return None.

Examples

prices = [20, 15, 15, 30, 20, 25]
k = 3
returns 25
prices = [3, 3, 5]
k = 2
returns 5
prices = [9, 9, 9]
k = 2
returns None

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run