COMPSCI 61A Lecture Notes - Lecture 7: Fibonacci Number, Pseudocode

41 views6 pages
zogo39484755 and 6 others unlocked
COMPSCI 61A Full Course Notes
22
COMPSCI 61A Full Course Notes
Verified Note
22 documents

Document Summary

When you make a function call, you need to wait for it to return before you can do anything else print(n) print(n) cascade(n//10) print(n) if n < 10: Two definitions of cascade def cascade(n): def cascade(n): if n < 10: Two implementations are equally clear, shorter is better. In this case, longer implementation is more clear (at least to denero) When learning to write recursive functions, put base cases first. Both are recurisve functions, even thouhg only the first has typical structure. Example: inverse cascade def inv_cascade(n): def f_then_g(f, g, n): grow = lambda n: f_then_g(grow print (n//10) ) shrink = lambda n: f_then_g( print shrink (n//10) ) Tree recursion grow(n) print(n) shrink(n) if n: f (n) g(n) When one function makes more than one recursive call. Definition process: tree shaped processes whenever execucuting the body of a recursive functioin makes more than one recursivce call. 0, 1,1, 2, 3, 5, 8 9,227,465 def fib(n): if n == 0:

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents