Predict the output
What does this print?
vals = [1, 2, 3, 4, 5, 6]
half = (len(vals) + 1) // 2
front, back = vals[:half], vals[half:][::-1]
out = []
for i, v in enumerate(front):
out.append(v)
if i < len(back):
out.append(back[i])
print(out)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [1, 6, 2, 5, 3, 4]
Right: the front half alternates with the turned-around back half.
The list version shows the target shape; the chain version reaches it with the same split, turn and interleave steps without copying.
Why not the others?
- B [1, 4, 2, 5, 3, 6]. The back half has to be turned around first.
- C [6, 1, 5, 2, 4, 3]. The first link stays first.