Predict the output
Events arrive out of order. The consumer ignores older versions. What's the final state?
state = ("new", 0)
events = [("shipped", 3), ("paid", 2), ("created", 1)]
for status, v in events:
if v > state[1]:
state = (status, v)
print(state)Hint 1 of 1
Which events have v greater than what's applied?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A ('shipped', 3)
Correct. Versions 2 and 1 are older than 3, so they're ignored.
Comparing each event's version with the stored one makes the update monotonic: only a newer version applies. Version 3 arrives first, so the older 2 and 1 are skipped.
Why not the others?
- B
('created', 1). That's last-arrival-wins, the bug versions prevent. - C
('paid', 2). 2 isn't newer than 3. - D
('new', 0). Version 3 is newer than 0, so it applies.