I am working on ps3 and they're asking for two functions to iteratively and recursively count the number of matches of the key in the target. This seems too obvious for part a: def countSubStringMatch(target,key): print target.count(key) is this correct? and then secondly, I don't have any idea how to do it recursively. I tried to write a recursive function that would tell me exactly where the key is hiding in the target but it's not working:

Hey! We 've verified this expert answer for you, click below to unlock the details :)

I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!

def countSubStringMatchRecursive(target,key): y = find(target,key) print y while y!= -1: countSubStringMatchRecursive(target[y+1:len(target)],key)

Yes, the iterative function is that easy. I've been stuck on exactly the same problem with making it recursive. I actually spent an embarrassing amount of time weeding through ancient posts in this group, and I dug up the 4 codes on the link below. And just for kicks, I added your code, too. I modified the whole thing so that each one will print out some results so I can figure out what the heck they each did. I'm sure several other study groupies will recognize their own code: http://dpaste.com/hold/612083/ You will notice that the second one doesn't give the same results as #1, 3, and 4. Function 5 is your code (which seems to have an infinite loop in it, although it reports the correct answer as the second value). Because I don't quite understand how to make this problem recursive, I can't tell you why any of these 5 examples do or don't work. I'm kinda hung up on "from string import *" right now. Something else helpful I dug up in ancient questions was this: The assignment is to write functions for how many times the key occurs in a target. We're not asked to report the indices of those matches (which I spend a few days trying and failing to do). Please, if you figure it out, post it!

In my dpaste post, most everyone did this: return 1 + countSubStringMatchRecursive(target[nextindex:], key) I don't understand why they added one to the re-call of their functions. And BTW, this seems to be what tmd197's function is missing.

Looking for something else?

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

## More answers

Looking for something else?

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