Skip to content

Instantly share code, notes, and snippets.

@atomictom
Created January 15, 2015 05:39
Show Gist options
  • Select an option

  • Save atomictom/df09fb22eea7ceca4694 to your computer and use it in GitHub Desktop.

Select an option

Save atomictom/df09fb22eea7ceca4694 to your computer and use it in GitHub Desktop.
def make_state(name, states, transitions, is_final):
def state(string, index):
if index >= len(string):
return is_final
cur_input = string[index]
next_state = transitions[cur_input]
return states[next_state](string, index + 1)
states[name] = state
def main2():
states = {}
make_state("q0", states, {"0": "q0", "1": "q1"}, True)
make_state("q1", states, {"0": "q2", "1": "qc"}, False)
make_state("q2", states, {"0": "q1", "1": "q2"}, False)
make_state("qc", states, {"0": "q0", "1": "q1"}, False)
initial = "q0"
string = raw_input("Enter a string to test >>> ")
try:
result = states[initial](string, 0)
except:
print "Bad input"
else:
print "Accepted!" if result else "Rejected =("
main2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment