anonymous
  • anonymous
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?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
rsmith6559
  • rsmith6559
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.
anonymous
  • anonymous
@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.
rsmith6559
  • rsmith6559
The base case returns 0, and the others increment it before returning it.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
@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?
1 Attachment
rsmith6559
  • rsmith6559
def countSubStringMatchRecursive(target,key): # base case if( len( target ) == 0 ): return 0 else: # unwind return countSubStringMatchRecursive(target[i:],key) + 1

Looking for something else?

Not the answer you are looking for? Search for more explanations.