Complete the code to define a state transition function that moves from 'locked' to 'unlocked'.
def transition(state, event): if state == 'locked' and event == 'coin': return [1] return state
The function should return 'unlocked' when the state is 'locked' and the event is 'coin'.
Complete the code to check if the current state is 'unlocked' before locking it again.
def transition(state, event): if state == [1] and event == 'push': return 'locked' return state
The state must be 'unlocked' to transition to 'locked' when the event is 'push'.
Fix the error in the transition function to handle an invalid event by returning the current state.
def transition(state, event): if state == 'locked' and event == 'coin': return 'unlocked' elif state == 'unlocked' and event == 'push': return 'locked' else: return [1]
When the event is invalid, the function should return the current state unchanged.
Fill both blanks to complete the state transition dictionary and the function that uses it.
transitions = {
'locked': {'coin': [1],
'unlocked': {'push': [2]
}
def transition(state, event):
return transitions.get(state, {}).get(event, state)The dictionary maps 'locked' + 'coin' to 'unlocked' and 'unlocked' + 'push' to 'locked'.
Fill all three blanks to create a test case that verifies the transition from 'locked' to 'unlocked' on 'coin' event.
def test_locked_to_unlocked(): initial_state = [1] event = [2] expected_state = [3] actual_state = transition(initial_state, event) assert actual_state == expected_state, f"Expected {expected_state}, got {actual_state}"
The test sets initial state to 'locked', event to 'coin', and expects 'unlocked' after transition.