ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
from collections import Counter
def pairs_eq(a, t):
c = Counter(a)
total = 0
for x in c:
y = t - x
if x < y:
total += c[x] * c[y]
elif x == y:
total += c[x] * (c[x] - 1) // 2
return total
print(pairs_eq([1, 2, 2, 3, 3, 3, 4], 5), pairs_eq([2, 2, 2], 4))- A. 6 3
- B. 7 3
- C. 7 2
- D. 8 3
Correct answer: B. 7 3
Count value pairs using multiplicities. 1 with 4 gives 1 pair, and 2 with 3 gives 2 x 3 = 6 pairs, for a total of 7. Three equal values 2 give C(3,2) = 3 pairs.
More Python Programming questions
- What is printed? python import bisect def minlen(a, s): pre = [0] for x in a: pre.append(pre[-1] + x) best = float('inf') for i in…
- What is printed? python people = [('Ann', 30), ('Bob', 25), ('Cid', 30), ('Dan', 25)] s1 = sorted(people, key=lambda p: p[0]) s2 =…
- What is printed? python from itertools import accumulate h = [3, 1, 4, 1, 5, 9, 2, 6] vis = [x for x, m in zip(h, [0] + list(accumulate(h…
- For an array of 10^6 numbers, which approach finds the largest sum of k consecutive elements fastest and correctly?
- Which statement about the bisect module is FALSE?
- What is printed? python def mink(piles, h): lo, hi = 1, max(piles) while lo < hi: mid = (lo + hi) // 2 if sum(-(-p // mid) for p in piles)…