Fill the blank
Fill the blank to get the last index of 5.
from bisect import bisect_left, bisect_right
vals = [1, 5, 5, 5, 8]
first = bisect_left(vals, 5)
last = ____
print(first, last)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A bisect_right(vals, 5) - 1
Right: bisect_right lands just after the last 5.
The two bisect calls bracket the run of fives, so the last copy is one before the right bracket.
Why not the others?
- B bisect_right(vals, 5). That is one past the last copy.
- C bisect_left(vals, 5) + 1. That assumes exactly two copies.