Review the AI
The k Smallest
Three solutions; one goes wrong with repeated numbers. Label them.Problem
k_smallest(values, k): return the k smallest numbers as a list, smallest first. Repeats count separately: [3, 1, 1, 2], k = 2 → [1, 1].Solution A
1def k_smallest(values, k):
2 return sorted(set(values))[:k]
Solution B
1def k_smallest(values, k):
2 values = list(values)
3 out = []
4 for _ in range(k):
5 m = min(values)
6 values.remove(m)
7 out.append(m)
8 return out
Solution C
1import heapq
2
3def k_smallest(values, k):
4 return heapq.nsmallest(k, values)