ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: C. 1 4 3

bisect_left returns the first index where 2 could go (1), and bisect_right returns the index after the last 2 (4). Their difference counts the copies of 2.

More Python Programming questions

Exams that ask this topic