Skip to content

Instantly share code, notes, and snippets.

@smmoosavi
Last active January 3, 2016 00:49
Show Gist options
  • Save smmoosavi/8384852 to your computer and use it in GitHub Desktop.
Save smmoosavi/8384852 to your computer and use it in GitHub Desktop.
One line finite-state machine implementation
# State machine implementation:
olfsm = lambda Q, input, init: reduce(lambda o, i: o + (o[-1] if i not in Q[o[-1]] else Q[o[-1]][i]), input, init)
from olfsm import olfsm
# Example transition matrix
Q = {
'1': {'c': '4', 'b': '3'},
'2': {'a': '1'},
'3': {'a': '1'},
'4': {'c': '3', 'b': '2'}
}
# Example inputs
I = list('aacabcaabacccabacabaccccaaabacbbcaac')
#example usage
print olfsm(Q,I,'1')
# output: 1114422113143313144214333111314222114
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment