combining data structures (class slides)

CSC 110 – Combining data structures

Announcements

  • Fill out SCS at scsonline.ucatt.arizona.edu
  • If 80% of students complete the survey, I’ll replace one of your lowest quizzes grade with full points (5/5 pts)
  • Quiz 07 this Wednesday (combining data structures)
  • Midterm 3 on April 24, Final exam on May 3

Tuples as dictionary keys

  • We can only use immutable types as dictionary keys (strings, integers, and tuples)
  • Values in a dictionary can be of any type

Tuples as dictionary keys

Tuples are useful as keys since keys need to be unique (no repeated key is allowed)

course_schedule = { ("CSC 110", "section 1") : ("Gittings", "129b"),
                    ("CSC 110", "section 2") : ("Gittings", "129b"),
                    ("CSC 110", "section 3") : ("Gittings", "129b"),
                    ("CSC 120", "section 1") : ("Gittings", "129b"),
                    ("CSC 144", "section 1") : ("Social Sciences", "100"),
                    ("CSC 120", "section 2") : ("Gittings", "129b")}
  • How would you retrieve the room where CSC 144, section 1 takes place?
  • How would you retrieve all rooms for CSC 120?

Attendance

Go to gradescope and answer today’s attendance question

Write a function

  1. Its name is retrieve_room
  2. It takes three arguments: a dictionary with course information, a string for course code and number (e.g., "CSC 110") and a string for section (e.g., "section 3")
  3. It returns a string with the building and room numbers (concatenated)
course = "CSC 110"
section = "section 3"
print( retrieve_room(course_schedule, course, section) ) 
# "Gittings 129b"

Write a function – solution

def retrieve_room(course_dict, course, section):
  c, s =  course_dict[(course, section)]
  return c + " " + s

def main():
  course_schedule = { ("CSC 110", "section 1") : ("Gittings", "129b"),
                      ("CSC 110", "section 2") : ("Gittings", "129b"),
                      ("CSC 110", "section 3") : ("Gittings", "129b"),
                      ("CSC 120", "section 1") : ("Gittings", "129b"),
                      ("CSC 144", "section 1") : ("Social Sciences", "100"),
                      ("CSC 120", "section 2") : ("Gittings", "129b")}
  print( retrieve_room(course_schedule, 
                       "CSC 110", "section 3")) # "Gittings 129b"
  
main()
Gittings 129b

Dictionaries as values

course_schedule = { "CSC 110": 
                      {1: {"building": "Gittings", "room": "129b"},
                       2: {"building": "Gittings", "room": "129b"},
                       3: {"building": "Gittings", "room": "129b"} },
                    "CSC 120":
                       {1: {"building": "Gittings", "room": "129b"},
                        2: {"building": "Gittings", "room": "129b"} },
                    "CSC 144":
                       {1: {"building": "Social Sciences", "room": "100"}}}
                       
course_schedule["CSC 110"][3]["building"]
'Gittings'
  • How would you retrieve the room for the section 3 of CSC 110?