Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 13 Online
OpenStudy (anonymous):

So, I've got the iterative function for ps3a (miracle, I know) and as soon as I felt the pride of it I started writing the recursive version of the function and proceeded to receive a beat down. I thought I could picture what the recursive bit was doing but apparently I can't. Here's the code: http://codepad.org/FPciSiqT I couldn't get an output for the post BUT with the 'target,key' that's there ("asdfasdfasdfasdf","asdf") I am getting "No Match"

OpenStudy (anonymous):

There's an explanation at the bottom of the code. What I think is happening is that it's running through the recursion until there's nothing left and then printing "no match". I'm trying to increase the count through each recursion and then print it at the end but no luck. Is there a problem counting like I am? Should I "return count"? I'm going to play around with it some more...

OpenStudy (anonymous):

if you add print before you call the function codepad will give you output: http://codepad.org/axwfqOk1

OpenStudy (anonymous):

lol...noted. thanks.

OpenStudy (anonymous):

Take a look at this: http://codepad.org/qR1Iuh8o I think your problem is that in line 9 (of the the code I just pasted), the return "No Match" breaks out of the function and just ends it...

OpenStudy (anonymous):

Also, I don't know if line 12 is doing what you think it is.

OpenStudy (anonymous):

when a function calls itself (recursion) a new version of the function is created. each version is separate and 'self-contained'. count in version 1 of the function is not the same thing as count in version 2 of the function. this concept is called variable scope - i imagine there are good references to be found with a web search. read this through, i have read it more than a few times and still go back to it, it may help: http://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces if you want each version of your function to operate on an object you need to pass that object to each version of the function. if you want something to 'get-out-of' a function you have to return it - this seems especially important with a recursive function. for recursive functions it helps a lot to just write down in words the thing(s) you want it to do before you start. for this specific problem you may be able to find a solution that does not have a 'counting' variable.

OpenStudy (anonymous):

Go back to the recursive factorial function from lecture. Pay attention to the base case, recursive call (and the smaller version of the problem it passes to the recursive call), and what exactly is being returned. Make sure you fully understand it before trying to write your own recursive function. Recursion is a tricky topic, and it's hard to get recursive functions right unless it's really really on purpose.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!