Program to construct DFA accepting odd number of 0 s and odd number of 1 s | Function to check whether the given is accepted by DFA or not ; Stores initial state of DFA ; Stores final state of DFA ; Stores previous state of DFA ; Iterate through the string ; Checking for all combinations ; Update the previous_state ; If final state is reached ; Otherwise ; Driver Code ; Given string ; Function Call
def checkValidDFA ( s ) : NEW_LINE INDENT initial_state = 0 NEW_LINE final_state = 0 NEW_LINE previous_state = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ( s [ i ] == '0' and previous_state == 0 ) or ( s [ i ] == '1' and previous_state == 3 ) ) : NEW_LINE INDENT final_state = 1 NEW_LINE DEDENT elif ( ( s [ i ] == '0' and previous_state == 3 ) or ( s [ i ] == '1' and previous_state == 0 ) ) : NEW_LINE INDENT final_state = 2 NEW_LINE DEDENT elif ( ( s [ i ] == '0' and previous_state == 1 ) or ( s [ i ] == '1' and previous_state == 2 ) ) : NEW_LINE INDENT final_state = 0 NEW_LINE DEDENT elif ( ( s [ i ] == '0' and previous_state == 2 ) or ( s [ i ] == '1' and previous_state == 1 ) ) : NEW_LINE INDENT final_state = 3 NEW_LINE DEDENT previous_state = final_state NEW_LINE DEDENT if ( final_state == 3 ) : NEW_LINE INDENT print ( " Accepted " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Accepted " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "010011" NEW_LINE checkValidDFA ( s ) NEW_LINE DEDENT