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

h11: Review (tracing functions)

ready? assigned due points
true Thu 05/24 12:30PM Thu 05/31 12:30PM

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.


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) 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. 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) (20 pts)    ☐ (a)     ☐ (b)     ☐ (c)     ☐ (d)     ☐ (e)     ☐ (f)    

    >>> ====== 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)     ☐ (a)     ☐ (b)     ☐ (c)     ☐ (d)     ☐ (e)     ☐ (f)    

    >>> ====== 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) (25 pts)    ☐ (a)     ☐ (b)     ☐ (c)     ☐ (d)     ☐ (e)     ☐ (f)    

    >>> ====== 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) (25 pts)    ☐ (a)     ☐ (b)     ☐ (c)     ☐ (d)     ☐ (e)     ☐ (f)    

    >>> ====== 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