← Back to challenges

FSA: Creating Machines

PythonHarddata_structuresfunctional_programmingmath

Instructions

Create a [finite-state automaton] (https://en.wikipedia.org/wiki/Finite-state_machine) that determines whether a binary number is divisible by five. The finite-state automaton from [this challenge] (https://innokodakademija.com/challenge/mmiLWJzP3mvhjME7b) can be constructed as follows:

Example FSA

divisible = {
  "S0": ["S0", "S1"],
  "S1": ["S2", "S0"],
  "S2": ["S1", "S2"]
}

Each key is a state, and each value denotes instructions for each type of input. For "S0", the array ["S0", "S1"] indicates that if a 0 is received, the new state is "S0". If 1 is received, the new state is "S1".

Notes

  • Remember to create a dictionary, not a function.
  • In this case, "accept" would mean that the number is divisible by five, whereas "reject" means that it isn't.
  • The starting and accepting states should both be "S0".
  • The automaton should read the digits of a binary number from left to right. For example, the first digit for 26 (0b11010) would be 1, since we ignore the 0b.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.