1
h12
CS8 W18
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"

h12: Perkovic 6.1 (Dictionaries) and 6.2 (Sets)

ready? assigned due points
true Tue 02/27 02:00PM Tue 03/06 02:00PM

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 four lowest scores (if you have zeros, those are the four lowest scores).


READING ASSIGNMENT

Please read Perkovic 6.1 (Dictionaries) and 6.2 (Sets). Then complete these problems and turn in your completed homework in lecture on the due date.

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class. It is important to fill in both name and umail every time, since handwriting is sometimes difficult to decipher. Having both helps us ensure you get credit for your work. Also: while we strongly prefer that you submit your homework on a single sheet of paper passed out in lecture, if you MUST submit it on multiple sheets, JUST write your name at the top of bot sheets and turn in both sheets UNCONNECTED. DO NOT staple, paper clip, fold, or do ANYTHING that would make it difficult to automatically feed your paper through a scanner.
  2. Suppose we define the following Python variable:

    capitals = { "CA":"Sacramento", "NV":"Carson City",
    	     "AZ":"Phoenix", "WA":"Olympia", "OR":"Salem" }
    

    What would the value of each of the following Python expressions be? Fill in the blanks. Remember that type returns types in the format <class 'int'>, <class 'list'>, <class 'str'>, etc. Use the correct value for full credit.

    Pts Expresssion Value
    (5 pts) type(capitals)  
    (5 pts) capitals["CA"]  
    (5 pts) type(capitals["NV"])  
    (5 pts) len(capitals["WA"])  
    (5 pts) capitals["OR"][2:6]  
  3. Suppose we define states as follows (note that these definitions are not necessarily correct or complete):

    states = {
     "AZ" : { "capital" : "Phoenix", "borders" : {"AZ","NV"} },
     "CA" : { "capital" : "Sacramento", "borders" : {"AZ","NV","OR"} },
     "NV" : { "capital" : "Carson City", "borders" : {"OR","AZ","CA"} },
     "OR" : { "capital" : "Salem", "borders" : {"WA","CA","NV"} },
     "WA" : { "capital" : "Olympia", "borders" : {"OR"} }
    }
    
    
    

    What would the value of each of the following Python expressions be? Fill in the blanks. Remember that type returns types in the format <class 'int'>, <class 'list'>, <class 'str'>, etc. Use the correct value for full credit.

    1. Pts Expresssion Value
      (5 pts) type(states)  
      (5 pts) type(states["OR"])  
      (5 pts) type(states["OR"]["capital"])  
      (5 pts) type(states["CA"]["borders"])  
    2. Pts Expresssion Value
      (5 pts) len(states.keys())  
      (5 pts) len(states["OR"].keys())  
      (5 pts) len(states["NV"]["capital"])  
      (5 pts) states["OR"]["capital"]  
      (5 pts) states["AZ"]["borders"]  
    3. Pts Expresssion Value
      (5 pts) "CA" in states["CA"]["borders"]  
      (5 pts) "NV" in states["CA"]["borders"]  
      (5 pts) "AZ" in states["CA"]["borders"]  
      (5 pts) states["WA"]["borders"] < states["CA"]["borders"]