1 |
h05 |
CS8 W18 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | |||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h05: Perkovic 3.3 (User-Defined Functions), 3.4 (Python Variables and Assignments), 3.5 (Parameter Passing)
ready? | assigned | due | points |
---|---|---|---|
true | Tue 01/30 02:00PM | Tue 02/06 02:00PM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores).
READING ASSIGNMENT
Please read Perkovic 3.3 (User-Defined Functions), 3.4 (Python Variables and Assignments), 3.5 (Parameter Passing). Then complete these problems and turn in your completed homework in lecture on the due date.
- (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class. It is important to fill in both name and umail every time, since handwriting is sometimes difficult to decipher. Having both helps us ensure you get credit for your work. Also: while we strongly prefer that you submit your homework on a single sheet of paper passed out in lecture, if you MUST submit it on multiple sheets, JUST write your name at the top of bot sheets and turn in both sheets UNCONNECTED. DO NOT staple, paper clip, fold, or do ANYTHING that would make it difficult to automatically feed your paper through a scanner.
-
(20 pts) Page 68 lists this Python function definition that corresponds to the mathematical function
1 2 3
def f(x): res = x**2 + 1 return res
Note that we can also write this more simply without the
res
variable, saving a line of code.1 2
def f(x): return x**2 + 1
Which version is better? That’s a matter of programming style. Often, but not always, writing in fewer lines of code is preferred. With experience, you’ll gain a sense of when to do one vs. the other. For this problem, it is up to you which option to use.
In the space below, please write a function definition for the mathematical function
- The following is a function definition. Note that `def` is an abbreviation for definition:
Contrast this with a function call such as `f(3)`, `f(2 + 2)`, or `f(0.5)`. Those evaluate to 10, 17, and 1.25 respectively. Each of the problems below shows a function definition at left, then several function calls at right. For each function call, write what the function call evaluates to.
1 2
def f(x): return x**2 + 1
-
def foo(x): return 1 - x * 3
pts function call write result here (5 pts) foo(3)
(5 pts) foo(-1)
-
def bar(s): return s[2:4]
pts function call write result here (5 pts) bar("yellow")
(5 pts) bar("white")
-
def blerg(thing): return thing[-1]
pts function call write result here (5 pts) blerg("green")
(5 pts) blerg([1,2,3])
(5 pts) blerg(["UCSB","Stanford","UCLA"])
-
def fleek(a,b): return 2 * max(a,b)
pts function call write result here (5 pts) fleek(3,5)
(5 pts) fleek(7,1)
(5 pts) fleek("UCSB","UCLA")
-
- (10 pts) According to Chapter 3.3, what is a docstring?
-
(10 pts) Add an appropriate docstring to the Python function below that converts inches to centimeters.
def in2cm(inches): return inches * 2.54