ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import bisect print(bisect.bisect_left([(1, 'a'), (3, 'b'), (5, 'c')], 3, key=lambda t: t[0]))
- A. 1
- B. 0
- C. 2
- D. 3
Correct answer: A. 1
Since Python 3.10 bisect takes key=. The key is applied to list elements only, not to x, so you compare 3 with the keys 1, 3, 5. The first position where 3 fits is index 1.
More Python Programming questions
- 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…
- What is printed? python import heapq print(heapq.nlargest(2, ['pear', 'fig', 'apple'], key=len))
- What is printed? python import heapq h = [] for t in [(2, 'b'), (2, 'a'), (1, 'z')]: heapq.heappush(h, t) print([heapq.heappop(h) for in…