[Tutor] Recursion - Beginner

Emile van Sebille emile at fenx.com
Sat May 29 00:23:53 CEST 2010


On 5/28/2010 3:09 PM Shawn Blazer said...
> 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:

I imagine what you'd need to do is manually follow the code for glee 
step-by-step when the values (2,1) are passed into it, so I'd write 
something like:

-pass---idol----scrub--1stReturn--2ndReturn--3rdReturn
   1       2       1      n/a        n/a       pass2
   2       1       2      n/a        n/a       pass3
   3      -1       1

and continue until the the recursion completes.

>
> 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 )
>
> Also, I'm not really sure what a question like this is asking...
>

To answer that you'd need the code for getLeaves, which isn't here.  It 
must have been provided to you somewhere...

Emile



>>>> getLeaves ( jenny )
> [5,3,0,9]
>>>>
>>>> getLeaves ( joshua )
> [15,17,19,11,13]
>
> I know its really simple, but I'm really new to this and I'd really
> appreciate some help.
> Thanks!
>




More information about the Tutor mailing list