Predict the output
What does this print?
vals = [10, 20, 30, 40, 50]
k = 2
lead, trail = k - 1, -1
while lead < len(vals) - 1:
lead += 1
trail += 1
print(trail, vals[trail + 1])Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 2 40
Right: trail stops at index 2, just before 40, the second link from the end.
Index -1 plays the placeholder's role here: it lets trail start before the head, so the head can be removed with the same code.
Why not the others?
- B 3 50. Trail stays one behind the link being removed.
- C 1 30. The gap of k links puts trail directly before the target.