I have written the code for testing the input string if it satisfies the Non Deterministic finite state machine/automata. The logic seems fine to me but the code crashes because of stackoverflow because the recursive function is not returning.
The reason is dictionary is always returning same result for an input even though for the same input the dictionary has two different outputs.
def hello(curExist, curTemp, string, dict, accept):
print "curExist: ", curExist, " curTemp: ", curTemp, " Letter: ", string;
if(string == ""):
return curExist in accept;
elif((curTemp,string[0]) in dict):
curExist = curTemp = dict[(curTemp,string[0])];
return hello(curExist, curTemp, string[1:], dict, accept);
elif((curTemp,'') in dict):
curTemp = dict[(curTemp, '')];
return hello(curExist, curTemp, string, dict, accept);
elif((curExist, '') in dict):
curTemp = dict[(curExist, '')];
return hello(curExist, curTemp, string, dict, accept);
else:
return False;
dict={(1,'a'):2, (2,''):6, (6,''):4, (2,''):3, (3,'b'):4, (4,'c'):5};
mString = "ac";
accept=[5];
print hello(1,1,mString,dict,accept);
Console Output:
curExist: 1 curTemp: 1 Letter: ac
curExist: 2 curTemp: 2 Letter: c
curExist: 2 curTemp: 3 Letter: c
curExist: 2 curTemp: 3 Letter: c
curExist: 2 curTemp: 3 Letter: c // printing this untill crash
.
.
.
How can I solve this problem?