Fetch the Mug, One Event at a Time

finite-state machine · transition table · events · task logic

A kitchen robot fetches a mug and brings it back to its owner. Its task logic is a finite-state machine: at every moment it is in exactly one state, and a sensor event may move it to another state. It starts in "search". The transitions are:

state event next state
"search" "seen" "approach"
"approach" "lost" "search"
"approach" "close" "grasp"
"grasp" "holding" "return"
"grasp" "slip" "approach"
"return" "dropped" "search"
"return" "home" "done"

An event that has no row for the current state is ignored: the robot stays where it is. "done" has no rows at all, so once the mug is delivered nothing changes it.

Write fetch_states(events) that takes the list of events in the order they arrive and returns the list of states: the start state, then the state after each event (so the list is one longer than events).

Examples

Input:  events = ["seen", "close", "holding", "home"]
Output: ["search", "approach", "grasp", "return", "done"]

Input:  events = ["close", "seen", "lost", "seen", "close", "slip", "close", "holding", "dropped"]
Output: ["search", "search", "approach", "search", "approach", "grasp", "approach", "grasp", "return", "search"]
Explanation: "close" means nothing while searching, so it is ignored. The mug slips
once and is dropped on the way back, so the robot ends up searching again.

Input:  events = []
Output: ["search"]

Goals

  • Write a robot's task logic as a table of `(state, event) -> next state`
  • Run the machine over a stream of sensor events, ignoring events that do not apply
Starting Python…