Skip to content

Instantly share code, notes, and snippets.

@kevinslin
Created May 9, 2012 17:34
Show Gist options
  • Save kevinslin/2647025 to your computer and use it in GitHub Desktop.
Save kevinslin/2647025 to your computer and use it in GitHub Desktop.
A finite state automata
from optparse import OptionParser
from collections import defaultdict
def run(inputWord):
a, states = open("avtomat.txt") ,defaultdict(list)
state, final= a.readline().split()[1:],a.readline().split()[1:]
[states[(i.split()[0], i.split()[1])].append(i.split()[3]) for i in a]
for letter in inputWord:
state = [st for s in state for st in states[(s,letter)]]
return any(i in state for i in final)
print [(b,"YES") if run(b) else (b,"NO") for b in OptionParser().parse_args()[1]]
vtomat.txt:
# init: q0
# final: q2 q3
# q0 a -> q1
# q0 b -> q3
# q1 a -> q1
# q1 b -> q2
# q2 a -> q2
# q2 b -> q2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment