Predict the output
What does this print?
blocks, new = [[1, 2], [3, 5], [6, 7], [8, 10]], [4, 8]
left = [b for b in blocks if b[1] < new[0]]
right = [b for b in blocks if b[0] > new[1]]
mid = [b for b in blocks if b not in left and b not in right]
lo = min([new[0]] + [b[0] for b in mid])
hi = max([new[1]] + [b[1] for b in mid])
merged = [lo, hi]
print(left + [merged] + right)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [[1, 2], [3, 10]]
Right: [3, 5], [6, 7] and [8, 10] all touch [4, 8].
Only the middle group changes; the blocks on either side are copied as they are, which keeps the work linear.
Why not the others?
- B [[1, 2], [3, 8], [8, 10]]. [8, 10] touches the new booking at 8.
- C [[1, 2], [4, 8]]. The absorbed blocks widen the new one.