Predict the output
What does this print?
vals = [3, 0, 1]
n = len(vals)
acc = n
for i, v in enumerate(vals):
acc ^= i ^ v
print(acc, n * (n + 1) // 2 - sum(vals))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 2 2
Right: both methods find that ticket 2 is absent.
Starting from n covers the one index enumerate never produces, so every present ticket cancels exactly once.
Why not the others?
- B 2 3. The sum formula gives 6 - 4 = 2.
- C 3 2. Starting acc at n includes the last index, which the loop does not reach.