Skip to content

Instantly share code, notes, and snippets.

@SilvaEmerson
Last active May 28, 2019 15:56
Show Gist options
  • Save SilvaEmerson/904382457844cab46a4c7fa91f4078aa to your computer and use it in GitHub Desktop.
Save SilvaEmerson/904382457844cab46a4c7fa91f4078aa to your computer and use it in GitHub Desktop.

Usage

trans_func = {('q0', 'a,b') : 'q1',
	      ('q1','a,b'): 'q0'}

final_state = automata(organize_func(trans_func), 'aabb')
is_aceppeted(['q0'], final_state)
def organize_func(trans_func):
temp_dic = {}
for key in trans_func:
state = key[0]
entries = key[1].split(',')
temp_dic = {**temp_dic, **{(state, entry): trans_func[key] for entry in entries}}
return temp_dic
def automata(trans_func, word):
curr_state = 'q0'
for letter in word:
if curr_state:
curr_state = trans_func.get((curr_state, letter), False)
return curr_state
def is_aceppeted(final_states, last_state):
if last_state:
return last_state in final_states
return last_state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment