[Tutor] Recursion - Beginner

Alan Gauld alan.gauld at btinternet.com
Sat May 29 01:23:15 CEST 2010


"Shawn Blazer" <shawnblazer94 at gmail.com> wrote

> Hello! I'm a high school student, and I'm having some trouble 
> learning  recursion in my class...
> For example:
>
> Trace the sequence of recursive calls that glee(2,1) spawns:
>
> def glee ( idol , scrub ) :
> if idol == 0 :
> return scrub
> elif idol < 0 :
> return scrub + glee ( idol + 10 , idol % 3 )
> else :
> return scrub + glee ( idol - scrub , idol % 3 )

I'd start with a print statement right at the top of glee.
Something like

print "glee( idol=", idol,", scrub=", scrub, ")"

That will show you what haoppens with each call of glee.
>From there its a short step to tracing the return values etc.

> Also, I'm not really sure what a question like this is asking...
>
>>>> getLeaves ( jenny )
> [5,3,0,9]
>>>>
>>>> getLeaves ( joshua )
> [15,17,19,11,13]

Me neither - I don't even see a question?!

BTW
My tutorial has a topic on recursion, that might help...

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list