Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 41 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
DemonKinge: so I'm trying to figure something out is anyone on hea dating aliciaa?
5 seconds ago 47 Replies 4 Medals
marienne: guys pls i need you all to send me some good music
40 seconds ago 7 Replies 1 Medal
MakaylaChuck23: how can I deal with a controlling dad (any tips?)
9 hours ago 4 Replies 1 Medal
Gucchi: chem help
13 hours ago 3 Replies 0 Medals
MakaylaChuck23: math
12 hours ago 7 Replies 0 Medals
CecePitbull: i hav therapy today chat!!! and sum pictures!!!
6 hours ago 8 Replies 0 Medals
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!