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)
- A. [1, 4, 4, 4, 9] 3
- B. [1, 4, 4, 9] 2
- C. [1, 4, 4, 4, 9] 2
- 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
- What is printed? python from collections import deque d = deque(range(5)) d.rotate(-2) print(list(d), d[0], d[-1])
- What is printed? python import heapq h = [] for x in [5, 1, 3]: heapq.heappush(h, x) heapq.heappush(h, 0) print([heapq.heappop(h) for in…
- What is printed? python s = 'hello' print(s.index('l'), s.rfind('l'), s.find('l', 3), s.index('lo'))
- What is printed? python import itertools print(sum(1 for in itertools.combinations(range(6), 2)), sum(1 for in…
- Which of the following statements about Python's sort is correct for CP problems?
- What is printed? python def solve(n): if n == 0: return [] return solve(n // 2) + [n % 2] print(solve(6), solve(0))