Available online as http://www.cs.ucsb.edu/~pconrad/cs8/10F/homework/H09—printable PDF
Name: (4 pts) | Umail Address: (4 pts) | @umail.ucsb.edu |
Lab Section (2 pts)—circle one: | 9am | 10am | 11am | noon | unknown | crashing |
You may collaborate with at most ONE other person on this homework assignment. If you do, please enter his/her name here: | |
(He/she should also enter your name on her/his assignment.) |
This assignment is due in
Lecture on Thursday, 10.28.
It may ONLY be submitted in
Lecture, at 2pm on Thursday.
You must come IN PERSON to turn it in during
your assigned Lecture section.
>>> list("the quick fox")
>>> list(''the quick fox'')
"
vs. 2 single quotes ''
)
The reading assignment for this homework consists of two parts
The question on this side are based on the previous reading—so you should be able to tackle it right away. If you are unsure, review Sections 4.1-4.3 before asking your TA or instructor for help.
(10 pts) If we assign a variable x
, y
or z
to be equal to some list, we can determine the length of that list with the len
function, just like we do with strings (see the example in the left column below.)
Given that, please write the definition of a function hasLengthZero(x)
that returns True
if x
has length zero, and False
if it does not. Assume that x
is either a string or a list. Some example calls and test cases appear below.
Example of the len() function operating on lists: |
Test cases for the function you are writing ( hasLengthZero ) |
>>> x = ["Go", "Gauchos"] >>> y = [2,3,5,7,11,13,17] >>> z = ["Phill", "Conrad", 3,79, 5551212] >>> len(x) 2 >>> len(y) 7 >>> len(z) 5 >>> |
check_expect("test 1",hasLengthZero(""),True) check_expect("test 2",hasLengthZero(""),True) check_expect("test 3",hasLengthZero([3]),False) check_expect("test 4",hasLengthZero("foo"),False) |
Write only the function definition of hasLengthZero
Please turn over for more...
...continued from other side
The questions on this side are based on the new reading: Section 4.4 (pp. 129-131) in your textbook.
Please read that section, then answer below.
When we have a list, we need to distinguish between the indices* and the values.
As an example, for the list [16,23,17]
the indices of the list are 0,1,2
and the values in the list are 16, 23
and 17
.
We are going to need to distinguish between indices and values.
So,
to be sure you understand, let's practice. Suppose we set myList = [89,24,12,68]
myList
to a list of integers, e.g. myList = [34,2,67]
or myList = [3,7,19,2]
(5 pts) Now, continuing on p. 130-131, write a function getMin
that is similar to the getMax
from Listing 4.2—i.e. it uses iteration over indices.
(5 pts) Now write a function getMin
that is similar to the getMax
function from Listing 4.3—i.e. it uses iteration over values.
End of H09