Predict the output
What does this print?
from bisect import bisect_left, bisect_right, insort
scores = [10, 20, 20, 30]
insort(scores, 25)
print(scores, bisect_right(scores, 25) - bisect_left(scores, 20))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [10, 20, 20, 25, 30] 3
Right: 25 is inserted in order, and three scores lie in [20, 25].
Bisect brackets a value range in two O(log n) searches, while the insert itself still shifts the items after 25.
Why not the others?
- B [10, 20, 20, 30, 25] 3. insort keeps the list sorted.
- C [10, 20, 20, 25, 30] 2. Both copies of 20 count, plus 25.