Skip to content

Instantly share code, notes, and snippets.

@douglas-vaz
Created July 23, 2012 18:20
Show Gist options
  • Save douglas-vaz/3165183 to your computer and use it in GitHub Desktop.
Save douglas-vaz/3165183 to your computer and use it in GitHub Desktop.
Python DFA to validate strings containing "abac"
#!/usr/bin/python2
print "Enter a string"
str = raw_input()
state = 0
dfa = [{'a':1,'b':0,'c':0},{'b':2,'a':0,'c':0},{'a':3,'b':1,'c':0},{'c':4,'a':2,'b':1},{'a':4,'b':4,'c':4}]
for i in str:
print i,': ',state,' -> ',
state = dfa[state][i]
print state
if state == 4:
print "String belongs to the language"
else:
print "String does not belong to the language"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment