1
h05
CCS CS20 S18
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 04/17 12:30PM Mon 04/23 12:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

This assignment should be submitted by scanning the pages in the correct order to a PDF file and uploading to gradescope.com.

For more information, visit ucsb-cs8.github.io and look for Gradescope: Student Self Submission under "topics".

Even though it is a Gradescope submission, nevertheless, *please fill in the information at the top of this homework sheet*, including your name and umail address.


READING ASSIGNMENT

Please read Perkovic 3.3-3.5 (Functions). Then complete these problems.

  1. (10 pts) These 10 points are for filling in your name at the top of the sheet, and scanning correctly on Gradescope. (Having these here gives me a place to give you feedback if there is some glitch with the way you are doing it.)

  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