ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import bisect
def pairs_le(a, d):
a = sorted(a)
c = 0
for i, x in enumerate(a):
j = bisect.bisect_right(a, x + d, i + 1)
c += j - (i + 1)
return c
print(pairs_le([1, 5, 3, 8, 4], 2))- A. 3
- B. 4
- C. 5
- D. 6
Correct answer: B. 4
After sorting, each value counts the later values within distance d, found with bisect_right and a lower bound. The pairs (1,3), (3,4), (3,5) and (4,5) give 4.
More Python Programming questions
- What is printed? python import bisect def closest(a, x): i = bisect.bisectleft(a, x) cands = a[max(i - 1, 0):i + 1] return min(cands…
- What is printed? python def content(g, s): g.sort(); s.sort() i = j = 0 while i < len(g) and j < len(s): if s[j] >= g[i]: i += 1 j += 1…
- What is printed? python from collections import Counter def least(tasks, n): c = Counter(tasks) m = max(c.values()) k = sum(1 for v in…
- What is printed? python s = 'abcdefg' print(s[::-2], s[5:1:-2], s[-3::-3])
- What is printed? python a = {'x': 1, 'y': 2} b = {'y': 3, 'z': 4} c = a | b a.update(b, x=9) print(c, a)
- What is printed? python import heapq h = [5, 7, 9] heapq.heapify(h) r1 = heapq.heappushpop(h, 6) r2 = heapq.heappushpop(h, 1) print(r1…