ProgrammingModerateTCS NQT · Wipro pattern
What is printed?
What is printed?
def pairs(a, t):
a = sorted(a)
l, r = 0, len(a) - 1
out = []
while l < r:
s = a[l] + a[r]
if s == t:
out.append((a[l], a[r]))
l += 1
r -= 1
elif s < t:
l += 1
else:
r -= 1
return out
print(pairs([3, 1, 4, 2, 5, 6], 7))- A. [(1, 6), (2, 5)]
- B. [(1, 6), (3, 4)]
- C. [(2, 5), (3, 4), (1, 6)]
- D. [(1, 6), (2, 5), (3, 4)]
Correct answer: D. [(1, 6), (2, 5), (3, 4)]
On a sorted array the two pointers move toward each other and report every matching pair. Sorted input 1 to 6 with target 7 gives (1, 6), (2, 5) and (3, 4).
More Python Programming questions
- What is printed? python class N: def init(self, v, nxt=None): self.v, self.nxt = v, nxt def rev(h): prev = None while h: h.nxt, prev, h =…
- What is printed? python def cntpal(s): n = len(s) c = 0 for ctr in range(2 n - 1): l, r = ctr // 2, ctr // 2 + ctr % 2 while l >= 0 and r…
- What is printed? python import bisect a = [9, 7, 5, 3, 1] print(bisect.bisectleft(a, -6, key=lambda v: -v), bisect.bisectleft(a, 6…
- What is printed? python from collections import deque d = deque([1, 2, 3, 4, 5], maxlen=5) d.rotate(2) d.appendleft(0) print(list(d)…
- What is printed? python print(max(['bb', 'aa', 'c'], key=len), min(['bb', 'aa', 'c'], key=len))
- What is printed? python from collections import defaultdict words = ['eat', 'tea', 'tan', 'ate', 'nat', 'bat'] g = defaultdict(list) for w…