Posted on

This is a coursework for CS262 at Udacity. A Python simple implementation of finite state machine.

# FSM Simulation

edges = {(1, 'a') : 2,
         (2, 'a') : 2,
         (2, '1') : 3,
         (3, '1') : 3}

accepting = [3]

def fsmsim(string, current, edges, accepting):
    if string == "":
        return current in accepting
    else:
        letter = string[0]
        # QUIZ: You fill this out!
        # Is there a valid edge?
        # If so, take it.
        # If not, return False.
        # Hint: recursion.
        entry = (current, letter)
        if entry not in edges:
            return False
        return fsmsim(string[1:], edges[entry], edges, accepting)


print(fsmsim("aaa111",1,edges,accepting))

Leave a Reply

Your email address will not be published. Required fields are marked *