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

Base case for recursion? I understand the concept of recursion, but for Problem Set 3 problem 1 part b I'm having a hard time coming up with a base case. Any hints? Thanks.

OpenStudy (anonymous):

how about "if the keystring is NOT in the targetstring/current section of the targetstring, return 0/something else that lets you stop calculating"

OpenStudy (anonymous):

I have something that works, but I can't figure out how to print out a number of instances. I can just print out 'one instance' per time the key is found. How can I print out the actual number of instances? def countSubStringMatchRecursive(target, key): index = 0 target_length = len(target) if find(target[index:target_length], key) == -1: # test to see if value is returned return 0 else: position = find(target[index:target_length], key) index = position + 1 print "one instance" return countSubStringMatchRecursive(target[index: target_length], key) I tried to return a counter variable that increments, but when i print it it always stays at 0. Thanks in advance. def countSubStringMatchRecursive(target, key): index = 0 target_length = len(target) counter = 0 if find(target[index:target_length], key) == -1: # test to see if value is returned print counter return 0 else: position = find(target[index:target_length], key) index = position + 1 counter += 1 print "one instance" return countSubStringMatchRecursive(target[index: target_length], key) and counter

OpenStudy (anonymous):

I think your problem lies in your IF statement. I think you need to use a WHILE loop...otherwise your code will choose one of your two paths, execute, then end. The while loop will look until your counter max is achieved.

OpenStudy (anonymous):

You can also try a FOR loop, as I did in my code. It works, but you have to have the right conditions. Make sure your conditions can be met, and make sure that it will iterate or recur for all instances of your key.

OpenStudy (anonymous):

I'm honestly still not that happy with my solutions to this problem set, so someone should jump in if I start talking nonsense. Basically, for this problem I think you need another parameter besides target and key. It would be a 0 int or an empty or nearly-empty list, and it would get iterated at each recursion. The pseudocode would look like: recursionfunction(target,key, counter): if the keystring is not in the targetstring: return the counter else: add one to the counter location = where it was found return the result of the recursionfunction with a shortened targetstring and the current key and counter

OpenStudy (anonymous):

Try this. No extra counting parameter needed. def countSubStringMatchRecursive(target, key): found_at = target.find(key) if found_at >=0: return 1+ countSubStringMatchRecursive(target[found_at+1:], key) else: return 0 # not found

OpenStudy (anonymous):

Yeah, that's better. Thanks!

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!
Latest Questions
Lilmunchin: Trump or Biden
10 seconds ago 3 Replies 0 Medals
ARTSMART: Art!
26 minutes ago 5 Replies 4 Medals
Jasonisyours: What were the key causes of the French Revolution in 1789?
20 minutes ago 3 Replies 5 Medals
PureSoulless: Why is the word "Pedophile" always censored in yt vids?
1 day ago 3 Replies 0 Medals
Jalli: What's 58x3634u00b07
22 hours ago 6 Replies 3 Medals
arriya: who wanna play roblox
1 day ago 5 Replies 1 Medal
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!