CS8, 09M, UCSB
H04: (Based on Miller/Ranum, Chapter 4 (up to p. 128) Total points: ? (printable PDF)
Accepted: on paper, in lab (either at 11am, or 12:30pm, in Cooper Lab) on Thursday, August 13th
No email submission allowed.
Name: (2 pts)______________________________ UCSBNetID (2 pts) _____________________
Lab Section (2 pts) Circle one: Thu 11am Thu 12:30pm Unknown
Circle the one you plan to attend, which may or may not be the one you are registered for.
Review p. 83-94 from Chapter 3 in your textbook, and then pages 117-128 from Chapter 4 p. 128.
Please note that there is a typo on p. 125: the third example of the list function should look like this, i.e. double quotes:
>>> list("the quick fox")
Not like this (two single quotes together):
>>> list(''the quick fox'')
Then answer these questions:
name to the value "Sonia"?name[-2] at the >>> prompt?name[2] ?name[ ] with some number, that would result in returing the final letter in "Sonia", that is the "a"? ["Go", "Gauchos"] is a list of strings[2,3,5,7,11,13,17] is a list of numbers (which all happen to be of type integer, or int in Python)["Phill", "Conrad", 3.79, 5551212] is a list of mixed data (some string, some float, and some integer)Please turn over for more problems
Continued from other side
(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 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 test cases appear in the right column below.
Example of the len() function operating on lists: |
Test cases for the function you are supposed to write |
>>> 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 >>> |
hasLengthZero("") returns TruehasLengthZero([]) returns TruehasLengthZero([3]) returns FalsehasLengthZero("foo") returns False |
isLong(s) that takes a string s as a parameter, and returns True if the length of that string has more than 5 characters, and returns False if it has 5 characters or fewer.|Test cases:
long("Gertrude") should return Truelong("Conrad") should return Truelong("Phill") should return Falselong("Fred") should return FalseHints: