1
h11
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"

h11: Review (tracing functions)

ready? assigned due points
true Tue 02/20 02:00PM Tue 02/27 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).


The problems below are all based on readings already completed. If you need to, review the appropriate sections in the textbook. 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. 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.
  2. For each of the following, there is a Python function definition in the box at right. Assume that it has been loaded into idle3 and that we’ve selected Run Module (or pressed F5.) Then we typed in the function call shown, and something is printed as a result. Which of the answers shown matches what is printed? (Multiple choice)

    (a) (10 pts)

    >>> ====== RESTART ======
    >>> 
    >>> mystery2([3,4,5,6,7])
    _______
    >>> 
    
    1. 4
    2. 7
    3. [4, 6]
    4. [7]
    5. Error:
      ...    result = result + x
      TypeError: can only concatenate
      list (not "int") to list
      
    6. None of the above
    def mystery2(aList):
        """
        Computes something from list.  What?
        """
    
        result = []
        for x in aList:
            if (x % 2 == 0):
                result = result + [x]
    
        return x
    

    For each of the following, there is a Python function definition in the box at right. Assume that it has been loaded into idle3 and that we’ve selected Run Module (or pressed F5.) Then we typed in the function call shown, and something is printed as a result. Which of the answers shown matches what is printed? (Multiple choice)

    (b) (20 pts)

    >>> ====== RESTART ======
    >>> 
    >>> mystery3([10,20,25])
    _______
    >>> 
    
    1. 10
    2. [10, 20]
    3. [25]
    4. 30
    5. Error:
      ...    result = result + x
      TypeError: can only concatenate
      list (not "int") to list
      
    6. None of the above
    def mystery3(aList):
        """
        Computes something from list.  What?
        """
    
        result = []
        for x in aList:
            if (x % 2 == 0):
                result = result + [x]
    
        return result
    

    (c) (20 pts)

    >>> ====== RESTART ======
    >>> 
    >>> mystery4([7,14,102,9])
    _______
    >>> 
    
    1. 9
    2. [14, 102]
    3. 144
    4. 0
    5. Error:
      ...    result = result + x
      TypeError: can only concatenate
      list (not "int") to list
      
    6. None of the above
    def mystery4(aList):
        """
        Computes something from list.  What?
        """
    
        result = []
        for x in aList:
            if (x % 2 == 0):
                result = result + [x]
    
            return result
    

    (d) (20 pts)

    >>> ====== RESTART ======
    >>> 
    >>> mystery5([7,14,102,9])
    _______
    >>> 
    
    1. 9
    2. [14, 102]
    3. 144
    4. 0
    5. Error:
      ...    result = result + x
      TypeError: can only concatenate
      list (not "int") to list
      
    6. None of the above
    def mystery5(aList):
        """
        Computes something from list.  What?
        """
    
        result = 0
        for x in aList:
            if (x % 2 == 0):
                result = result + 1
    
            return result
    

    (e) (20 pts)

    >>> ====== RESTART ======
    >>> 
    >>> mystery6([7,14,102,9])
    _______
    >>> 
    
    1. 9
    2. [14, 102]
    3. 144
    4. 0
    5. Error:
      ...    result = result + x
      TypeError: can only concatenate
      list (not "int") to list
      
    6. None of the above
    def mystery6(aList):
        """
        Computes something from list.  What?
        """
    
        result = 0
        for x in aList:
            if (x % 2 == 0):
                result = result + 1
    
        return result