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

h02: Perkovic 2.3 (Lists, Tuples)

ready? assigned due points
true Tue 04/17 12:30PM Fri 04/20 12:00PM

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.


READING ASSIGNMENT

Please read Perkovic 2.3 (Lists, Tuples). Then complete these problems.

  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. Section 2.3 describes lists and tuples in Python. Assuming the following assignment statements have been entered at the Python prompt:

    schools = ("UCSB","Stanford","UCLA","UCSD","Cal Tech")
    schedules = [ ["CMPSC 8","MATH 3A"], ["CMPSC 8","PSTAT 5A"],["MATH 3B"] ]
    units = [8, 8, 4]
    

    What would be the result of entering the following at the Python interactive shell prompt?

    (Note: You are encouraged to check your answers at the Python prompt before turning in your work, but try this on paper first, just by reading the text and trying to predict what will happen. Then try typing in the results at the Python prompt. Change your answers if they were mistaken, but even more important, try to figure out why you were incorrect.)

    Be very precise. Note that True is not the same in Python as true; upper vs. lower case matters. You will not get full credit for answers that are not precisely correct. And note that "UCSB" and ["UCSB"] are not the same in Python—one is a string, and the other is a list of length one containing a single string.

    Points Expression Result Points Expression Result
    (4 pts) len(schedules)   (4 pts) sum(units)  
    (4 pts) len(units)   (4 pts) "CMPSC 8" in schedules[0]  
    (4 pts) len(schedules[1])   (4 pts) "UCSD" in schools  
    (4 pts) len(schools[-1])   (4 pts) "U" in schools[1]  
    (4 pts) min(units)   (4 pts) "MATH 3A" in schedules  
  3. (5 pts) As described in section 2.3, lists and tuples are similar, but there are two big differences. One is that lists are written with square brackets [] while tuples are written with parentheses (). What is the other big difference between lists and tuples?

  4. (5 pts) Write a line of Python code that assigns the variable mySchool to have a value that is a tuple of length 1, containing the single string "UCSB".

  5. Assume that the following sequence of statements has been entered at the Python prompt. Note that subsequent statements may change the value of the variables (e.g. colors is altered by the call to the append method)

    colors = ["red","red","green"]
    colors.append("blue")
    colors.reverse()
    
    Points Expression Result Points Expression Result
    (4 pts) len(colors)   (4 pts) colors[-1][-1]  
    (4 pts) colors[0]   (4 pts) colors[2][0]  
    (4 pts) colors.count("red")   (4 pts) "blue" not in colors  
    (4 pts) colors.count("blue")   (4 pts) colors.count(colors[-1])  
    (4 pts) len(colors[1])   (4 pts) "e" in colors[0]