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?