Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
Zico13
Group Title
About the 2008 6.00 OCW, in problem set 3 (Q1), how do I count the number of instances of the key in the target string using recursion?
 one year ago
 one year ago
Zico13 Group Title
About the 2008 6.00 OCW, in problem set 3 (Q1), how do I count the number of instances of the key in the target string using recursion?
 one year ago
 one year ago

This Question is Open

rsmith6559 Group TitleBest ResponseYou've already chosen the best response.0
Generally, a recursive function has three parts. The "base case", which is when to stop calling the function, in this case when the end of the string is reached. Code for what you want to do as you're recursing. Before the base case is reached, and more recursion needs to be done. Code for what you want to do as the recursion "unwinds". After the base case is reached and the recursions are returning. Thinking recursively is tough to get used to, but well worth the effort.
 one year ago

Zico13 Group TitleBest ResponseYou've already chosen the best response.0
@rsmith6559: Well, I do know the logical steps of recursion; I'm unsure of how to count the no. of recursions that occur. Thanks anyway.
 one year ago

rsmith6559 Group TitleBest ResponseYou've already chosen the best response.0
The base case returns 0, and the others increment it before returning it.
 one year ago

Zico13 Group TitleBest ResponseYou've already chosen the best response.0
@rsmith6559: But again when the recursion stops and control falls back to the base step, the returned value is 0!! :( Could you please check my test file?
 one year ago

rsmith6559 Group TitleBest ResponseYou've already chosen the best response.0
def countSubStringMatchRecursive(target,key): # base case if( len( target ) == 0 ): return 0 else: # unwind return countSubStringMatchRecursive(target[i:],key) + 1
 one year ago
See more questions >>>
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.