Predict the output
What does this print?
log = [1, 1, 2, 2, 2, 5]
w = 1
for r in range(1, len(log)):
if log[r] != log[w - 1]:
log[w] = log[r]
w += 1
print(log[:w], w)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [1, 2, 5] 3
Right: three distinct readings are packed at the front.
The tail beyond w still holds old values; only the first w slots are the answer, which is why the real function trims it.
Why not the others?
- B [1, 2, 2, 5] 4. Each repeat of 2 equals the last kept value and is skipped.
- C [1, 1, 2, 2, 2, 5] 6. The write pointer only advances on a new value.