ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
from bisect import bisect_left, bisect_right
a = [1, 2, 2, 2, 3]
print(bisect_left(a, 2), bisect_right(a, 2), bisect_left(a, 0), bisect_right(a, 4))
  1. A. 1 3 0 5
  2. B. 1 4 0 5
  3. C. 1 4 0 4
  4. D. 0 4 0 5

Correct answer: B. 1 4 0 5

bisect_left gives the first index where 2 can go, which is 1, and bisect_right gives the position after the last 2, which is 4. Values past the end insert at the length of the list.

More Python Programming questions

Exams that ask this topic