Complete the code to define the main storage in event sourcing.
events = [] # This list stores all [1]
In event sourcing, all changes are stored as events in an append-only log.
Complete the code to rebuild the current state from events.
state = initial_state for event in events: state = [1](state, event)
The apply_event function updates the state by applying each event in order.
Fix the error in the event storage function to append new events.
def store_event(event): [1].append(event)
New events must be appended to the events list to keep the event history.
Fill both blanks to create a snapshot after every 100 events.
if len(events) [1] 100 == 0: snapshot = [2](state)
The modulo operator % checks if 100 events have occurred, then create_snapshot saves the current state.
Fill both blanks to implement event replay with snapshot optimization.
def rebuild_state(): state = [1] for event in events[[2]:]: state = apply_event(state, event) # Apply remaining events return state # Return the latest state
Start from the latest snapshot, replay events after the snapshot index len(snapshot_events).
