Predict the output
What does this print?
hops = [2, 3, 1, 1, 4]
jumps = layer_end = furthest = 0
for i in range(len(hops) - 1):
furthest = max(furthest, i + hops[i])
if i == layer_end:
jumps += 1
layer_end = furthest
print(jumps)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 2
Right: tile 0 to tile 1, then tile 1 to the end.
Each layer boundary marks one hop, and the furthest reach inside a layer sets the next boundary.
Why not the others?
- B 3. Hopping to tile 1 first reaches the end in one more hop.
- C 1. Tile 0 reaches tiles 1 and 2, not the last tile (4), so one hop is not enough.