ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import bisect
def span(a, x):
lo = bisect.bisect_left(a, x)
if lo == len(a) or a[lo] != x:
return (-1, -1)
return (lo, bisect.bisect_right(a, x) - 1)
print(span([1, 2, 2, 2, 5], 2), span([1, 2, 2, 2, 5], 3))- A. (1, 4) (-1, -1)
- B. (1, 3) (-1, -1)
- C. (1, 3) (2, 2)
- D. (0, 3) (-1, -1)
Correct answer: B. (1, 3) (-1, -1)
bisect_left finds the first position of x, and bisect_right minus one finds the last. The value 3 is absent, so the function returns (-1, -1).
More Python Programming questions
- What is printed? python def palsub(s): n = len(s) best = '' for c in range(2 n - 1): l, r = c // 2, c // 2 + c % 2 while l >= 0 and r < n…
- What is printed? python def partition(s): last = {ch: i for i, ch in enumerate(s)} out = [] start = end = 0 for i, ch in enumerate(s): end…
- What is printed? python def units(boxes, cap): boxes = sorted(boxes, key=lambda b: -b[1]) total = 0 for n, u in boxes: take = min(n, cap)…
- What is printed? python def minremove(iv): iv = sorted(iv, key=lambda x: x[1]) keep = 0 end = float('-inf') for s, e in iv: if s >= end…
- What is printed? python def longest(s): st = [-1] best = 0 for i, ch in enumerate(s): if ch == '(': st.append(i) else: st.pop() if not st…
- What is printed? python import heapq from collections import Counter words = 'a b c a b a d d d d'.split() c = Counter(words)…