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

A simple count loop inside Problem 3:2 is driving me insane. I swear I've tried every configuration known to mankind, and it's still not working properly. I know I'm just missing something silly...anybody see what it is? http://dpaste.com/hold/573637/

OpenStudy (anonymous):

well, it's kinda working - it does find and count all the substrings. but that information doesn't get 'back out' each (recursion) instance of stringSearchSub has it own instance of matches and when each of those function instances 'is forgotten' its instance of matches goes away too.. run this, i changed line 21- http://dpaste.com/573773/ notice the pattern of the printout. the first instance of the function prints the first place that key is found, then the second instance prints the second place that key is found ... till key is not found - then the last function instance prints out the id() of its matches variable, then the next-to-last function instance prints out the id() of its matches variable ...finally the first function instance prints out the id() of its matches variable usually, i have seen a recursive function return something. you should get in the habit of specifying a 'base case' - a situation where the function does not recurse, it just returns - this will help prevent infinite recursion. here is one (!) way to make your function work: if the key is not found, return 0 if key is found return 1 plus the return value of 'a recursion' http://dpaste.com/573767/

OpenStudy (anonymous):

bwCA is one of the enlightenedMinds...so just follow his advice :D

OpenStudy (anonymous):

I didn't solve this problem with recursion, so not sure if helps to look at my solution not using recursion. http://dpaste.com/573814/

OpenStudy (anonymous):

Thanks bwCA! You know that little nagging voice you get that says "Um, you're going to have to change the structure of this thing. Really, I mean it..."? I've been ignoring it, trying to force that intitial "if !=" construction to work. Bleh. Finally convinced, I shall restructure forthwith. Oh, and the id() trick will come in handy in future debugging....more thanks. mk95, seeing your solution was also very helpful. Nicely done, clean and concise, no Byzantine thought digressions like mine. Interesting watching people's brains work, huh? You can see it in their coding very clearly....

OpenStudy (anonymous):

it kinda bothered me that i changed yours so much then i realized that yours just needed a very small tweak - no need to change the structure. http://dpaste.com/575098/

OpenStudy (anonymous):

Aha! That does return the number of matches now; I see what you've done. Of course, I still have to use a print statement outside the function (I've gone on to the next lessons and have discovered the dubious joy of working in the test harness) but this solution fixes the original problem perfectly....

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!