I'm just getting back into this course after being busy for a while, and I'm on pset3. For the first part, I can't even think of a way to do it iteratively, but I've got some code for a recursive function.
Right now that's returning None. I'm not even sure how that could happen right now in my code. Any help?
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.
from string import *
count = 0
if find(target,key) != -1:
count=1+countSubStringMatchRecursive(target[find(target,key) + 1:],key)
this is my recursive. Yours looks closer to an iterative version
here is an iterative version:
from string import *
while index <(limit):
When you recurse you don't preserve the value of count
Not the answer you are looking for? Search for more explanations.
Or ... when you 'return from a recursion' you don't do anything with count.
Not sure which is the correct way to say that
Remember all Python functions must return something. They will return whatever you've told them to return if you use a return statement, but they will return None if that is not the case. Whenever a function is called the execution of the main code freezes and will not continue until the function has returned a value, no matter how long that takes. If it has nothing to return, it just returns the value None, rather than nothing at all.
bwCA is right, you're not really incrementing `count`, you just creating a bunch of instances of the same function, each with its own independent, local variable by that name. I'm not sure what jpkita meant when they said that your code was looking more like an iterative function, it looks pretty obvious to me that it is a recursive function, just a bit of a buggy one. I think they might have meant that you're incrementing count as though it's in an iterative loop. That's a serious problem with your function you'll have to think on a bit.