-3

I have two implementations of a non-deterministic finite-state machine:

common definitions

edges = { (1, 'a') : [2, 3],    #this was the finite state machine 
          (2, 'a') : [2],
          (3, 'b') : [4, 3],
          (4, 'c') : [5] ,}
accepting = [2, 5]  #when it comes to the state of 2 or 5 then return true

implementation 1

def nfsmsim(string, current, edges, accepting):   #style 1
    if string == "":
        return current in accepting
    else:
        letter = string[0]
        if (current,letter) in edges:
            remain = string[1:]
            for val in edges[(current,letter)]:
                if nfsmsim(remain, val, edges, accepting ) :
                    return True

        else:  #this one was deleted
            return False;   #indent this line

implementation 2

def nfsmsim(string, current, edges, accepting): #style 2
    if string == "":
        return current in accepting
    else:
        letter = string[0]
        if (current,letter) in edges:
            remain = string[1:]
            for val in edges[(current,letter)]:
                if nfsmsim(r, val, edges, accepting ) :
                    return True
        return False;

I expected that the two implementations would produce the same results, but they don't. Why not?

Community
  • 1
  • 1
李航宇
  • 1
  • 3
  • 3
    In your style2, `if nfsmsim(r, val, edges, accepting ) :`, the variable `r` is not defined. Coult that typo be your issue? – DainDwarf Feb 05 '16 at 09:40
  • Agree with @DainDwarf the value of `r` in your function call `nfsmsim(r, val, edges, accepting) is empty in the second verstion. You should ensure it continues to link up with the value of `remain` otherwise, the continuity of the string process you're doing gets lost. – Thomas Kimber Feb 05 '16 at 10:06
  • it's not r it's remain sorry I made mistakes while typing and it's still different of the two style – 李航宇 Feb 05 '16 at 10:19

1 Answers1

0

Implementation 1 can return nothing from the loop for val in edges[(current,letter)]: if the if nfsmsim test is never true when the for loop ends there is no return value.

Implementation 2 always return a True/False value because if the if nfsmsimtest is never true when the for loop ends there is a return False