Mark As Completed Discussion

Code Example

To show how this works in essence, we're going to implement the following state machine:

Code Example

This would accept strings like these:

  • abc
  • aaaaaabc
  • aaabaababc
  • etc.

We're going to use a hash map to record possible transitions. A pair of states and input triggering a particular transition is going to be the key, and the next state is going to be value. You can see how our key corresponds to arrows on our diagram, while the value corresponds to circles - states.

Please look over the code now.

Note that in practice we'll probably have more levels of abstraction than this. States might be entire classes instead of simple characters or strings and transitions may produce way more interesting effects than just printing output. But the essence is here - you could expand this code into any state machine you'd like.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment