# note - not quite done... states = ('q1', 'q2', 'q3', 'q4') start = 'q1' accept = ('q4',) s = input() # if I am at character i, can I reach an accepting state? def doesAccept(current_i, currentState): # did we reach the end of the string? if current_i >= len(s): if currentState in accept: return True else: return False currentLetter = s[current_i] if currentState == 'q1': if currentLetter == '0': return doesAccept(current_i+1, 'q1') elif currentLetter == '1': return doesAccept(current_i+1, 'q1') or doesAccept(current_i+1, 'q2') elif currentState == 'q2': # note - epsilon transition if doesAccept(current_i, 'q3'): return True if currentLetter == '0': return doesAccept(current_i+1, 'q3') elif currentState == 'q3': if currentLetter == '1': return doesAccept(current_i+1, 'q4') elif currentState == 'q4': if currentLetter == '0': return doesAccept(current_i+1, 'q4') if currentLetter == '1': return doesAccept(current_i+1, 'q4') # note - if transition wasn't defined, return False return False if doesAccept(0, start): print("accept"); else: print("reject") # note - practice for you... you could do ... ''' delta = [ # currentState,currentLetter,nextState ('q1','0','q1'), ('q1','1','q1'), ('q1','1','q2'), ... ] and then generic doesAccept that takes delta as parameter '''