1
h05
CS8 F17
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-3.5 (Functions)

ready? assigned due points
true Tue 10/17 08:00AM Tue 10/24 08:00AM

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.
There is NO MAKEUP for missed assignments, and you may not submit work in advance, or on behalf of another person.
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-3.5 (Functions). Then complete these problems and turn in your completed homework during your registered lab section..

  1. (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, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED.

    DO NOT staple, paper clip, spit-fold-and-tear, or do ANYTHING that would make it difficult to automatically feed your paper through a scanner.

  2. (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

  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 * 2 
      
      pts function call write result here
      (5 pts) foo(3)  
      (5 pts) foo(-1)  
    2. def bar(s):
         return s[1:3]
      
      pts function call write result here
      (5 pts) bar("blue")  
      (5 pts) bar("green")  
    3. def blerg(thing):
         return thing[-1]
      
      pts function call write result here
      (5 pts) blerg("blue")  
      (5 pts) blerg([1,2,3])  
      (5 pts) blerg(["UCSB","Stanford","UCLA"])  
    4. def fleek(a,b):
         return 2 * min(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, 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