CS61A Lecture 04: Higher-Order Functions

2 Pages
94 Views

Department
Computer Science
Course Code
COMPSCI 61A
Professor
De Nero

This preview shows 80% of the first page. Sign up to view the full 2 pages of the document.
Description
Fibonacci Sequence def fib(n) """"For n >= 2 predecessor, current = 0 , 1 # First 2 Fib numbers k = 2 # Tracks which Fib # is current while k < n: predecessor, current = current, predecessor + current k = k + 1 return current def choose(total, selection): ways = 1 selected = 0 while selected < selection: selected = selected + 1 ways, total = ways * total // selected, total - 1 return ways Designing Functions Characteristics of Function: A function's domain is set of all inputs possibly take as argument A function's range is set of all output values it returns A function's behavior is relationship it creates between input & output Give each function exactly one job (DRY, Don't repeat yourself) Implement process just one, execute many times Define function generally Default Arguments (feature of Python) use only one argument by defining after ( price, sales_tax=0.09) after(5) Generalize Patterns with Argument Regular Geometric shape relate lengths and area Assert 3 > 2, ' math is broken today' Assert 2 > 3, 'I said 2 is bigger' Use assert to create error messages Order of functions don't matter Higher Order Functions Common structure among functions may be computational p
More Less
Unlock Document

Only 80% of the first page are available for preview. Some parts have been intentionally blurred.

Unlock Document
You're Reading a Preview

Unlock to view full version

Unlock Document

Log In


OR

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?
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.


Submit