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__)
- A. 1 4 3 1 5 1 float
- B. 1 3 2 1 6 1 float
- C. 1 4 3 1 6 4 int
- 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
- What is printed? python from itertools import groupby s = "aabbbaac" g1 = [(k, len(list(v))) for k, v in groupby(s)] g2 = {k: len(list(v))…
- What is printed? python print("abc" 2 + "d", len("abc" 0))
- What is printed? python x = [10, 20, 30, 40, 50] print(x[-1], x[len(x) // 2], x[-2:])
- What is printed? python s = "Python" print(s.find("th"), s.find("z"), s.upper()[1:3])
- What is printed? python s = "a-b-c-d" print(s.split("-", 2), s.rsplit("-", 1))
- What is printed? python print("hello world".title(), "ab".center(6, ""), "42".zfill(5), "-7".zfill(4))