All quizzesHard
Ordering & Aliasing — Series 2
Preview — 3 of 10 questions
What is the output of the following code?
javascript
import heapq
nums = [3, 1, 4, 1, 5]
heapq.heapify(nums)
result = heapq.heapreplace(nums, 0)
print(result)
print(nums[0])A0, then 1
B1, then 1 — heapreplace only pushes the new value; the pop happens on a separate, later call.
C3, then 0 — heapreplace always removes the first element of the original list, not the true minimum.
D1, then 0
What is the output of the following code?
javascript
import bisect
nums = [1, 3, 5, 7]
bisect.insort(nums, 4)
print(nums)
bisect.insort(nums, 1)
print(nums)A[1, 3, 4, 5, 7], then [1, 1, 3, 4, 5, 7]
B[1, 3, 5, 7, 4], then [1, 1, 3, 5, 7, 4] — insort appends the new value at the end rather than inserting it in sorted position.
C[4, 1, 3, 5, 7], then [1, 4, 1, 3, 5, 7]
DTypeError: insort() requires a sorted list argument to be passed explicitly
What is the output of the following code?
javascript
data = [("a", 2), ("b", 1), ("c", 2), ("d", 1)]
result = sorted(data, key=lambda x: x[1], reverse=True)
print(result)A[('c', 2), ('a', 2), ('d', 1), ('b', 1)] — reverse=True also reverses the relative order of tied elements.
B[('a', 2), ('c', 2), ('b', 1), ('d', 1)]
C[('a', 2), ('b', 1), ('c', 2), ('d', 1)] — this is just the original, unsorted order.
D[('d', 1), ('b', 1), ('c', 2), ('a', 2)]
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.