Im trying to understand double recursion but i'm not getting anywhere.
def f(s):
if len(s) <= 1:
return s
return f(s[1:]) + s[0]
print f('world')
dlrow
if am right the above code will do the following:
- f(s[1:]) + s[0] ==> f(orld) + w
- f(s[1:]) + s[0] ==> f(rld) + o
- f(s[1:]) + s[0] ==> f(ld) + r
- f(s[1:]) + s[0] ==> f(d) + l <== here len(s) == 1 so s = d,
then:
- d + l = dl
- dl + r = dlr
- dlr + o = dlro
- dlro + w = dlrow
so dlrow will be printed as seen above.
I can not understand the code when you make it double recursive:
def f(s):
if len(s) <= 1:
return s
else:
return f(f(s[1:])) + s[0] #Note double recursion
print f('world')
Can someone please explain to me how this double recursion works?
y = f(f(x))istemp = f(x); y = f(temp).