I am a bit confused on how the compiler does recursion if it is in a variable. The only way to easily explain the question is if I show an example.
def recur_var(s1, s2):
'''Test for recursion in variables.'''
if s1 == '':
return s2
elif s2 == '':
return s1
else:
test = recur_var(s1[:-1], s2[:-1])
if s1[-1] == '1' and s2[-1] == '1':
return True
return test
The only recursion done in the above code is inside the variable that is above in priority over everything else, besides base cases.
I realize this code is all over the place in terms of what it does, but my question is, in the trace of this stack, does test only ever become the leftovers of the slicing?
Does test
do the recursion all the way down to the base cases without ever checking if s1[-1]
and s2[-1]
are both 1
? In other words, does it 100% ignore everything beneath it when it calls, or does it ignore itself and follow the rest of the code, then call?
I realize this was probably asked and worded awfully, but I'm very confused myself so I don't know a better way.
If you put the line:
right after the recursive call and right before
if s1[-1] == '1' and s2[-1] == '1':
, typical output is:So you only get to testing the top-level
s1, s2
(what you see in the final line) after the recursion has finished. In earlier stages of the recursion that test is run, but with smaller strings that are slices of the original string.Every time you call
recur_var
you create new strings with…[:-1]
. But they don't overwrite thes1
/s2
variables already existing in the methods. So the check in the next line are actually done on the original values which got passed to the function call.Basically every time you call this function it creates a new set of variables (so
s1
,s2
andtest
in your case). For example:This will output 2 to 0 and each call will have its own
x
. And you can assign to them all you want and it won't do a thing to the variables in the other function calls.For example it first calls
foo(3)
and then it reducesx
by 1 and callsfoo(x)
which isfoo(2)
and in there it reducesx
by 1 again, butx
in the context offoo(3)
is still 2.Now the exceptions are global variables and if you modify a variable (doing
x = 42
does not modifyx
but it overwritesx
).SHORT ANSWER
Yes, the function recurs on the shortened strings before it checks the end characters. It does that checking only after it exhausts one of the strings -- as it returns back down the call stack.
CAVEATS
In addition, you haven't described what your routine is supposed to do. What it actually does is to find out whether a character '1' appears at the same distance from the end of the two given strings. If so, it returns True; otherwise, it returns the first (abs(len(s1) - len(s2)) characters of the longer string. This is very strange behaviour, returning two different data types.
TRACING YOUR EXECUTION
Learn some debugging techniques. They'll serve you well as you continue programming.
To work on your program, I did two things:
This looks like:
This looks like:
... and the output from these tests is ...
This should let you figure out everything you need to know.