Created
July 23, 2012 18:20
-
-
Save douglas-vaz/3165183 to your computer and use it in GitHub Desktop.
Python DFA to validate strings containing "abac"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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