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))
- A. 0 4 4
- B. 2 4 2
- C. 1 4 3
- 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
- What is printed? python import bisect print(bisect.bisectleft([5, 4, 3], 4))
- What is printed? python import bisect print(bisect.bisectleft([(1, 'a'), (3, 'b'), (5, 'c')], 3, key=lambda t: t[0]))
- What is printed? python import bisect a = [(1, 'x'), (4, 'y')] bisect.insort(a, (3, 'z'), key=lambda t: t[0]) print(a)
- What is printed? python import heapq h = [1, 3, 5] r = heapq.heapreplace(h, 0) print(r, sorted(h))
- What is printed? python import heapq h = [1, 3, 5] r = heapq.heappushpop(h, 0) print(r, sorted(h))
- What is printed? python import heapq h = [] try: heapq.heappush(h, (1, {'a': 1})) heapq.heappush(h, (1, {'b': 2})) except TypeError as e…