Predict the output
What does this print?
from collections import Counter
belt = [1, 2, 1, 2, 3, 3, 3]
counts: Counter[int] = Counter()
left = best = 0
for right, kind in enumerate(belt):
counts[kind] += 1
while len(counts) > 2:
counts[belt[left]] -= 1
if counts[belt[left]] == 0:
del counts[belt[left]]
left += 1
best = max(best, right - left + 1)
print(best)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 4
Right: [1, 2, 1, 2] and [2, 3, 3, 3] both have length 4.
Removing kinds until two remain keeps every window valid, and two different stretches tie for the best length.
Why not the others?
- B 5. Any stretch of 5 here contains all three kinds.
- C 3. The first four items use only two kinds.