1 |
h11 |
CS8 M19-A |
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" |
h11: Review (tracing functions)
ready? | assigned | due | points |
---|---|---|---|
true | Tue 07/16 09:30AM | Tue 07/23 09:30AM |
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.)
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.
- (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.
-
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 selectedRun 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]) _______ >>>
4
7
[4, 6]
[7]
- Error:
... result = result + x TypeError: can only concatenate list (not "int") to list
- 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 selectedRun 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]) _______ >>>
10
[10, 20]
[25]
30
- Error:
... result = result + x TypeError: can only concatenate list (not "int") to list
- 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]) _______ >>>
9
[14, 102]
144
0
- Error:
... result = result + x TypeError: can only concatenate list (not "int") to list
- 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]) _______ >>>
9
[14, 102]
144
0
- Error:
... result = result + x TypeError: can only concatenate list (not "int") to list
- 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]) _______ >>>
9
[14, 102]
144
0
- Error:
... result = result + x TypeError: can only concatenate list (not "int") to list
- 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