Search code examples
pythonregexregular-languagefinite-automatacomputation-theory

validate that deterministic finite automata transition function has exactly one output state for each unique input alphabet


Suppose my DFA has the following structure:

dfa = DFA(
    {'q0','q1'}, # States
    {'0','1'}, # Alphabets
    {'q0':{'0':'q0', '1': 'q1'},
     'q1':{'0':'q1', '1': 'q0'}}, # Transition_funcs
    'q0', # Start_state
    {'q1'} # Final_state
    )

What I'm trying to do is to validate that for each state, my transition table has one and only one transition function for each alphabet or sigma. What I did is the following:

class DFA(object):
    def __init__(self, States=None, Alphabets=None, Transitio_funcs=None, Start_state=None, Final_states=None):
        self.States = Sates
        self.Alphabets = Alphabets
        self.Transition_funcs = Transition_funcs
        self.Start_state = Start_state
        self.Final_state = Final_state

    def validate(self):

        input = {}
        output = {}

        for k,v in self.Transition_funcs.items():
            for k1,v1 in v.items():
                input[k1] = count.get(k1,0) +1
                output[v1] = count.get(v1,0) +1
                for ks,vals in input:
                    if vals != 1
                        return False
                for ks,vals in output:
                    if vals != 1
                        return False   


        return True    

My understanding is that since each unique input has unique output state, I need to check there is only one count for each input alphabet and only one count for each output state. However,I'm getting following error:

if vals != 1
           ^
SyntaxError: invalid syntax

I would appreciate if anyone can point me to what exactly I'm missing.


Solution

  • You are missing : in both of your if statements, hence SyntaxError.

    It should read,

    if vals != 1: