ProgrammingVery hardProduct company OA

What is printed?

What is printed?
import bisect
a = [1, 3, 3, 3, 7]
i, j = bisect.bisect_left(a, 3), bisect.bisect_right(a, 3)
bisect.insort_left(a, 3.0)
k = a.index(3.0)
print(i, j, j - i, bisect.bisect(a, 2), bisect.bisect_left(a, 8), k, type(a[1]).__name__)
  1. A. 1 4 3 1 5 1 float
  2. B. 1 3 2 1 6 1 float
  3. C. 1 4 3 1 6 4 int
  4. D. 1 4 3 1 6 1 float

Correct answer: D. 1 4 3 1 6 1 float

bisect_left returns the first index whose element is ≥ 3 (1); bisect_right the first index whose element is > 3 (4); their difference is the number of 3s. insort_left puts 3.0 BEFORE the equal ints, at index 1, so the list now has 6 elements: index(3.0) = 1 (3 == 3.0 anyway) and a[1] really is the float. bisect(a, 2) = 1 and searching for 8 returns len(a) = 6, not 5. insort (right) would have put the float at index 4.

More Python Programming questions

Exams that ask this topic