A4 Python for engineers · Practice
State machines
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
Write a class Debouncer(n) whose step(x) takes a 0 or 1 sample, changes the debounced state only after n = 3 samples in a row that differ from it, and returns True exactly when the state changes from 0 to 1. Then set presses to the number of presses it finds in the starter's samples raw.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write frames(stream) for the framing in the second example (0x7E, a length, the data, then the XOR checksum of the data), returning a list of (data_bytes, ok) pairs. Set got = frames(stream) for the stream the starter picks.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A vending machine takes coins of 5, 10 and 25 cents. Its state is the credit so far. When the credit reaches price = 50 cents it dispenses, returns the change, and goes back to zero credit. Write vend(coins, price) returning (items, change): how many items it dispensed, and the list of change amounts it returned, for the coins the starter picks.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write count_matches(bits, pattern) that counts every place a pattern of bits occurs in a stream, overlaps included (so 1, 0, 1, 0, 1 holds the pattern 1, 0, 1 twice), by stepping a state machine one bit at a time: the state is how many bits of the pattern currently match. Set n = count_matches(bits, pattern) for the starter's stream and pattern.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A traffic light holds green for g = 5 s, yellow for y = 2 s and red for r = 6 s, starting green. Using a table of durations and successors, set colors to the list of its color ("GREEN", "YELLOW" or "RED") at each whole second from 0 to 59, and red_frac to the fraction of those seconds that are red.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A toggle switch flips a lamp each time it's pressed. Starting with the lamp off, set lamp to its state (True for on) after n = 5 presses, using a loop that steps the state.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The table NEXT maps (state, letter) to the next state of a machine that accepts the word "ON". Starting in "S0", step it through the string s the starter picks and set accepted to whether it ends in "DONE". Inputs not in the table send it back to "S0".
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in A4