I'm stuck on assignment 3 part 1. I wrote a recursive counter but I don't know how to print during just the last iteration of the function. Right now it prints every time the function is called. Can anyone give me advice or check out my code
def countSubStringMatchRecursive(target, key): """Counts recursively the number of times key appears in target""" global count count = 0 if len(target) > len(key): countSubStringMatchRecursive(target[1:], key) if find(target[:len(key)],key) > -1: count += 1 print count
you should use return and print result out of the function. code ought to looks like http://codepad.org/pSumamsh
when posting code pls use dpaste.com or pastebin.com or ideone.com ...... be sure to check syntax highliting and select Python - here is yours (with a minor mod): http://pastebin.com/0QCvyXE7 you can use the return statement in a function to return a value. you could use 'return count' in yours instead of 'print count' then call it like this: print countSubStringMatchRecursive(target, key).
return countSubStringMatchRecursive(blah) + 1 if it's sort of confusing why using this works, (which i didn't understand at first either) here's why: Basically imagine your entire function nested within that function call, and it'll add 1 to the function call. Now it'll keep on nesting itself again and adding 1 to the function call. When it reaches the last recursion and doesn't find anything, it'll return value zero. recur(blah4) + 1 recur(blah3) + 1 recur(blah2) + 1 recur(blah1) + 1 recur(blah1) = 0 then it can return a total of 4. Hope it helps, otherwise Bury!
@Rodic Thanks for the help. Got my code cleaned up and working. @bwCA Thanks for the advice. I'll use pastebin in the future. I think your code might have a problem depending on how they want it to evaluate a target "aaa" with key "aa". For example your code would yield 1 (since you are advancing the whole key length), while mine would yield 2. Any idea which answer should be the correct one? @cloudlife13 thanks, do u have any literature where it states that. I figured that out from experimenting, but never found anywhere that actually stated it
Naw, someone answered my question, but I still couldn't understand how you could add 1 to a function call and how that could count the number of matches. I was stumped and as I was going through this website, I saw someone's actual code. Then after looking and thinking about it, it finally dawned on me how it worked. It works all because eventually the function call hits an actual value, then adds back up. I guess another way to show it is like: (((0)+1)+1)+1 lol, almost mindblowing Keeping on chugging away, I'm at ps9 and I'm starting to see how kool/useful programming can be.
Cloudlife, is this your first programming experience? Also how long has it taken you to reach ps9? Have you been supplementing this course with anything else? Thanks in advance
I had a hard time with recursion, too, until I figured out the +1 thing. Actually, my programming friends tell me that recursion is a very difficult concept. So just give it time to soak in.
Join our real-time social learning platform and learn together with your friends!