Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

when I used find(target,'') #empty string it returned zero(0) but when I applied it(empty str) to subStringMatchExact I kinda run into infinite loop.is there anyway to fix that bug?

OpenStudy (maitre_kaio):

Could you show the code ? It would help to understand your question.

OpenStudy (anonymous):

http://dpaste.com/703498/ use any of functions with key '' subStrME('sas','') nothing comes out

OpenStudy (maitre_kaio):

print find('sas','') -> 0 So index is never incremented. A solution is to get rid of these special cases first. For example, assert the key is not the empty string.

OpenStudy (anonymous):

Pay attention, when you implement a recursive function you have to return something everytime it calls the function. In your code you just do it once at the end. Keep in mind that when you get to countSubStringMatchRecursive (target[(index+len(key)):], key) it starts again and don't return anything. I did it this way: def countSubStringMatchRecursive(target, key): index = find(target, key) if index > -1: return countSubStringMatchRecursive(target[index + 1:], key) + 1 return 0 Think about it. For me it took a lot of time and struggling before I understood something about recursion and could write this code. It will be essential once you get to Dynamic programming. I use target[index + 1:] instead of target[(index+len(key)):] so it also finds overlapping sequences, for example: it counts ana 2 times in banana. For DNA matching I did not know what we were expected to count. I hope it helps and sorry if it was not what you were looking for.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!