1
h05
CS8 M19-B
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
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 08/13 02:00PM Tue 08/20 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 lowest scores (if you have zeros, those are the 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.

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts () in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
  2. (20 pts) Page 68 lists this Python function definition that corresponds to the mathematical function f(x) = x^2 + 1

    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 g(x) = 3x^3 - 2x^2 + 1

  3. The following is a function definition. Note that `def` is an abbreviation for definition:
    1
    2
    
    def f(x):
       return x**2 + 1
    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. def foo(x):
         return 1 - x * 3 
      
      pts function call write result here
      (5 pts) foo(3)  
      (5 pts) foo(-1)  
    2. def bar(s):
         return s[2:4]
      
      pts function call write result here
      (5 pts) bar("yellow")  
      (5 pts) bar("white")  
    3. 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"])  
    4. 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")  
  4. (10 pts) According to Chapter 3.3, what is a docstring?
  5. (10 pts) Add an appropriate docstring to the Python function below that converts inches to centimeters.

    def in2cm(inches):
       return inches * 2.54