Department

Computer Science

Course Code

CSC148H5

Professor

Fiona Bawl

CODE/NOTE TAKEN FROM DISCUSSION BOARD WHICH WAS REDISTRIBUTED

Tracing Recursion

- Trace the simplest case

- Then trace the next most complex case (plug in known results)

Code

def rec_max (lst):

'''(list of int, can be nested) -> int

Return max number in possible nested list of numbers.

>>> rec_max([17,21, 0])

21

>>> rec_max ([17,[21,24],0])

24

'''

nums = []

#never to make a recursive call ? every elemnt has to be an int(no nesting)

for element in lst:

if isinstance(element, int): #always will be true ( no recursion )

nums.append(element)

else:

nums.append(rec_max(element))

return max(nums)

Tracing Recursion that you Should do( refer to the code above)

Base case for this function: list of ints #no nesting

rec_max([21,24]) is 24

rec_max([17,[21,24],0]) #base case for function = [21, 24]

rec_max([17,[21,24],0]

nums= []

first element = 17 #goes into sublist

next thing in the list is [21, 24] # but already know that its 24 ( no recursive calls)

last element of the list is int 0 # so append into num

therefore num = [17, 24, 0]

Once the for loop is done ( it returns the max of the sublist you make)

max(nuns) is 24, so 24 is returned

Solve simplest problems first then use these to solve the more complex solutions.

(this is how you TRACE but computer goes top down, biggest more complex cases

first then does the simplest ones)

Over 90% improved by at least one letter grade.

OneClass has been such a huge help in my studies at UofT especially since I am a transfer student. OneClass is the study buddy I never had before and definitely gives me the extra push to get from a B to an A!

Leah â€” University of Toronto

Balancing social life With academics can be difficult, that is why I'm so glad that OneClass is out there where I can find the top notes for all of my classes. Now I can be the all-star student I want to be.

Saarim â€” University of Michigan

As a college student living on a college budget, I love how easy it is to earn gift cards just by submitting my notes.

Jenna â€” University of Wisconsin

OneClass has allowed me to catch up with my most difficult course! #lifesaver

Anne â€” University of California

Join OneClass

Access over 10 million pages of study

documents for 1.3 million courses.

Sign up

Join to view

OR

By registering, I agree to the
Terms
and
Privacy Policies

Already have an account?
Log in

Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.