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

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

ready? assigned due points
true Thu 05/24 12:30PM Thu 05/31 12:30PM

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 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) 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. 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 indicated below. (Note that these definitions are not necessarily correct or complete in terms of US Geography, but they are valid Python code.)

    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"]