6 questions. Covers Partial sorting in Python.
Questions 1 to 5 of 6
heappop
smallest_by_selection
min(k, len(copy))
python def smallest_by_selection(a, k): copy = list(a) limit = min(k, len(copy)) for i in range(limit): best = i for j in range(i + 1, len(copy)): if copy[j] < copy[best]: best = j copy[i], copy[best] = copy[best], copy[i] return copy[:limit] print(smallest_by_selection([10, 2, 8, 2, 7], 3))
python import heapq wrong = [] for x in [3, 8, 5, 1, 7]: heapq.heappush(wrong, x) if len(wrong) > 2: heapq.heappop(wrong) print(sorted(wrong))
5 question(s) left on this page.