Fill the blank
Fill the blank.
def merge(a: list[int], b: list[int]) -> list[int]:
i = j = 0
out: list[int] = []
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 ____
print(merge([1, 4], [2, 3, 9]))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A out + a[i:] + b[j:]
Right: at most one of the two tails is non-empty, and both are already sorted.
After the loop one shelf is exhausted, so appending both tails costs nothing extra and keeps the order.
Why not the others?
- B out. That drops whatever is left on the longer shelf.
- C sorted(out).
outis already sorted; the leftovers are still missing.