State machines: apply the rules each step
A simulation question describes a thing that changes step by step and asks where it ends up. The recipe is always the same:
- Decide what the state is (a level, a mode, a position and a direction).
- For each step, apply the rule for the current state and the current input.
- Decide what happens to inputs the rules do not mention. If the statement says "ignored", do nothing for them.
A dictionary makes a tidy rule table:
next_mode = {"off": "dim", "dim": "bright", "bright": "off"}
mode = next_mode[mode]Most lost points here come from the rules at the edges: a level that would go below zero, a step that happens while the machine is locked, a command nobody defined. Read the statement once just to list those edges, then code.
In the visual, watch mode change once per tap and come back around.
See it run
mode = "off" cycle = {"off": "dim", "dim": "bright", "bright": "off"} for tap in range(4): mode = cycle[mode] print(mode)
A lamp with three modes. The dictionary is the whole rule table: current mode in, next mode out.
Practice state machines: apply the rules each step
Exercise 1
A water tank holds at most 10 litres. It starts with start litres (0 ≤ start ≤ 10). Each word in the list steps is applied in order:
"fill"adds 2 litres, but the level never goes above 10."drain"removes 3 litres, but the level never goes below 0.- Any other word (such as
"hold") changes nothing.
Return the final level as an integer.
Constraints: 0 ≤ len(steps) ≤ 1000.
Example: solution(4, ["fill", "fill", "drain"]) returns 5 (4 → 6 → 8 → 5).
Exercise 2
A keypad lock starts locked with 0 wrong tries. The list events holds the words below. After each event the lock is in one of three states: "locked", "open" or "alarm". Return a list with the state after every event, in order.
Rules:
- Locked +
"right": becomes open, wrong tries reset to 0. - Locked +
"wrong": wrong tries go up by 1. When they reach 3 the state becomes alarm. - Open +
"close": becomes locked. - Alarm +
"reset": becomes locked, wrong tries reset to 0. - Every other combination (a wrong code while open,
"right"during an alarm,"reset"when not in alarm, or a word not listed) changes nothing.
Constraints: 0 ≤ len(events) ≤ 1000.
Example: solution(["wrong", "right", "close"]) returns ["locked", "open", "locked"].
Exercise 3
A marker sits on a row of n cells numbered 0 to n-1. It starts on cell 0 and faces right. In each of k steps:
- If the cell in front exists, the marker moves onto it.
- Otherwise (it is on the last cell facing right, or on cell 0 facing left) it does not move; it only turns around to face the other way.
Return the cell number the marker is on after k steps.
Constraints: 1 ≤ n ≤ 100, 0 ≤ k ≤ 1000.
Example: solution(3, 4) returns 1. The marker goes 0 → 1 → 2, turns around on cell 2 without moving (step 3), then moves to cell 1 (step 4).
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.