## tmd197 4 years ago 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:

1. tmd197

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

2. JulieNewbie

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!

3. JulieNewbie

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.

4. carlsmith

Julie, do you know what people mean by basecase?

5. JulieNewbie

the base case does not make a recursive call.

6. carlsmith

That's right; it's what stops the recursion. If you have a function that calls itself: def f(): return f() When you call it, `f()`, the interpretor will stop executing the code around the function call and start executing the function definition. Which requires the interpretor to stop executing that definition, create another instance of `f`, then execute that, and so on, and so on, You'll just get loads of instances of the function, but they'll never return anything. So it'll be infinite recursion all the way. You need a basecase in the definition to stop it at some point. In order for that to happen, the argument passed will need to change with each new instance...

7. carlsmith

Take the following function: def f(n): if n == 2: return 2 else: return n * f(n - 1) Have a look at it, I'll try and explain...

8. JulieNewbie

Isn't that what this does: countSubStringMatchRecursive4(target[target.find(key)+1:] I get adding one inside the index. The function that worked in my dpaste was: countSubStringMatchRecursive4(target[target.find(key)+1:] + 1 Why add one to the whole thing? Wouldn't that give you the index + 1, which would then be one more than the correct answer?

9. carlsmith

If I call it, passing, lets say 3: f(3) because 3 is not equal to two, the else part will be executed. That'll create a new instance of `f`, passing n - 1, which would be 2. This will also prevent the first instance from returning anything for now. The new instance, passed 2, will return 2, because that's what it does whenever it's passed 2. It returns 2 to the first instance, which is still waiting to know what f(n -1) is. It is 2, so the first instance returns n * 2, or 3 * 2, which is 6 of course. If I pass a bigger number, say 100, it'll just take longer, as it'll spawn a long chain of instances of the function `f`, each waiting for the one that gets passed 2, to return 2, allowing it to reel back in, garbage collecting each instance of the function as it returns, so that, eventually, the first one called, can return 100 * 99 * 98 * 97 ... 3 * 2

10. carlsmith

f(4) return 4 * f(3) f(3) return 3 * f(2) f(2) return 2

11. carlsmith

4 * 3 * 2

12. carlsmith

13. JulieNewbie

Thank you, carlsmith. You are a very patient man! Yes, I understand why you want to add one to it. But it seems like the successful codes in my dpaste added one to the index AND one to the whole thing. That seems redundant.

14. carlsmith

Ah, see, I can't read the code. I'm not sure what it's meant to do. This line... countSubStringMatchRecursive4(target[target.find(key)+1:] should throw a syntax error, as should... countSubStringMatchRecursive4(target[target.find(key)+1:] + 1 They both, lack a closing paren.

15. carlsmith

Sorry, Julie, you'll have to post the code again mate.

16. JulieNewbie

Hmmm... don't know where it went. Let's try that again: http://dpaste.com/hold/612100/

17. carlsmith

I'll have a look now.

18. carlsmith

Which one works?

19. JulieNewbie

#1, #3, and #4

20. carlsmith

k

21. carlsmith

The third one has a one one the end, because it doesn't have it at the front of the expression. return countSubStringMatchRecursive4(target[target.find(key)+1:],key)+1 return 1 + countSubStringMatchRecursive4(target[target.find(key)+1:],key) The 1 represents the match that must have been found if the else block is being executed.

22. carlsmith

It's pretty ugly code to be honest. You should use nicer names for things, don't listen to the geeks. def count_matches(string, substring): if string.find(substring) == -1: return 0 else: return 1 + count_matches(string[ string.find(substring)+1: ], substring) Try and keep it clean and tidy.

23. JulieNewbie

Shame I can only give you one medal :*

24. carlsmith

Thank you. Have a go at it, but if you want to learn recursion, which, to be honest, isn't that useful very often, you'd still be better of starting with numbers, little ones. Iterating around the place at the same time, just make's it too confusing. My function above will take the number of cards in a deck and return the odds to one of the deck being shuffled and ending up in some predetermined order. For a regular deck, the odds are simply 52*51*50...4*3*2. That's all it does, but it shows how recursion works without many moving parts.

25. carlsmith

Let me know if you're still stuck. I skipped recursion when I first started and came back to it. It's painful at first.

26. walkerp1

Recursion is a neat concept, but it isn't suitable for really deep applications. A quick test shows: def recurse_check(calls): calls[0] += 1 recurse_check(calls) calls=[0] try: recurse_check(calls) except RuntimeError: print 'Number of calls:',calls[0] Output: Number of calls: 999

27. bwCA

of all the things they could have taught in an intro class, i wonder why they decided to include recursion?

28. JulieNewbie

I'm glad to hear it isn't a simple concept. I find iteration WAY more intuitive. Oh, and sorry to tmd197 for hijacking your question ;) I'm hoping all this chatter was helpful for you, too.