ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: A. [1, 4, 4, 4, 9] 3

bisect_left gives index 1, so the new 4 goes in front of the existing 4s. There are now three 4s, and bisect_right minus bisect_left counts them as 4 - 1 = 3.

More Python Programming questions

Exams that ask this topic