ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def merge(a, b):
out, i, j = [], 0, 0
while i < len(a) and j < len(b):
if a[i] <= b[j]:
out.append(a[i]); i += 1
else:
out.append(b[j]); j += 1
return out + a[i:] + b[j:]
print(merge([1, 4, 9], [2, 3, 10, 11]))- A. [1, 4, 9, 2, 3, 10, 11]
- B. [1, 2, 3, 4, 9, 11]
- C. [1, 2, 3, 4, 9, 10, 11]
- D. [1, 2, 3, 4, 9]
Correct answer: C. [1, 2, 3, 4, 9, 10, 11]
This is the merge step of merge sort. The left list is drained and then the rest of the right list is appended. The leftover tail of b is 10 and 11.
More Python Programming questions
- Which change removes the RecursionError for a DFS over a path of 10^5 nodes, while keeping the same visiting order?
- What is printed? python words = ['apple', 'bob', 'kayak', 'tree'] print([w for w in words if w == w[::-1]], len(words[1:3]))
- What is printed? python lines = ['3 4', '10 20'] pairs = [tuple(map(int, ln.split())) for ln in lines] print(pairs, sum(a b for a, b in…
- What is printed? python from math import comb print(comb(4, 2) comb(4, 2), comb(8, 4) - comb(8, 3))
- What is printed? python print(list(map(lambda p: p[0] + p[1], zip([1, 2], [10, 20, 30]))))
- What is printed? python s = 'a1b2c3' letters = ''.join(c for c in s if c.isalpha()) digits = sum(int(c) for c in s if c.isdigit())…