CS8, 10F, H09, due Thu Lecture 10.28—Miller/Ranum 4.4 (pp. 129-131).—Total points: ?

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.


Note: there is a typo on p. 125:
the third example of the list function should look like this:

>>> list("the quick fox")

Not like this

>>> list(''the quick fox'')


(double quotes " 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.

  1. (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

    • you don't need to write the definition of check_expect
    • you don't need to write any more test cases














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.

  1. (4 pts) The author refers to dispersion. In simple English, what is dispersion?


  2. *indices is just the plural of index. You'll also see indexes used sometimes.

    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]

    1. (4 pts) What are the indices of the myList ?


    2. (4 pts) What are the values in myList?


  3. The textbook shows two ways to write a getMax function—one that iterates "by index" (Listing 4.2), and another that iterates "by item" (Listing 4.3). Please look at these two functions, and notice how they work differently.

    We can also call these two techniques iterating over the indices (4.2) vs. iterating over the values (4.3)
    The second one, i.e. iterating by item, or over the values, is also sometimes called a for-each loop.

    Now, suppose that I set the variable myList to a list of integers, e.g. myList = [34,2,67] or myList = [3,7,19,2]

    1. (4 pts) Write a for loop that uses iteration over the indices (what the book calls "iteration by index") that will print every value in the list. Your loop needs to work no matter how big or small the list is.



    2. (4 pts) Now do the same thing—a for loop that prints every value in the list—but use the for-each style of loop.



  4. (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. (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