combining data structures (slides)

CSC 110 – Combining data structures

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

  • How would you retrieve the room where CSC 110, section 2 takes place?
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")}

Tuples as dictionary keys

  • How would you retrieve the room where CSC 110, section 2 takes place?
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")}
                    
course_schedule[("CSC 110", "section 2")]      
('Gittings', '129b')

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

Write a function

Its name is retrieve_room that takes three arguments:

  • a dictionary with course information

  • a string for course code and number (e.g., "CSC 110")

  • a string for section (e.g., "section 3")

It returns a string with the building and room number

course = "CSC 110"
section = "section 3"
assert retrieve_room(course_schedule, course, section) == "Gittings 129b"

Write a function – solution

def retrieve_room(course_dict, course, section):
  building, room =  course_dict[(course, section)]
  return building + " " + room

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")}
  course = "CSC 110"
  section = "section 3"
  assert retrieve_room(course_schedule, course, section) == "Gittings 129b"
  
main()

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 building for CSC 110 section 3?

Review

Write functions side-by-side

Write a function called remove_first that takes in a list of lists (2D list) and removes the first element of each sublist.

Write a function called remove_odds that takes in a list of list (2D list) of numbers and removes all values that are odd.

Solutions

def remove_first(list_of_lists):
  for sublist in list_of_lists:
    if len(sublist) > 0:
      sublist.pop(0)
  return list_of_lists

def remove_odds(list_of_lists):
  for sublist in list_of_lists:
    for i in range(len(sublist)-1, -1, -1):
      if sublist[i] % 2 == 1:
        sublist.pop(i)
  return list_of_lists

if __name__ == "__main__":
  original_list = [["banana", "apple"], [], ["pear"]]
  remove_first(original_list)
  assert original_list == [["apple"], [], []]
  
  numbers = [[1, 1, 1], [0, 0], []]
  remove_odds(numbers)
  assert numbers == [[], [0, 0], []]

Write functions side-by-side

Write a function called censor_consonants that takes as argument a string and returns the string with the consonants replaced with "*".

Write a function called replace_consonants that takes as argument a list of characters (strings of length one) and replaces the consonants with a "*".

Solutions

def censor_consonants(string):
  new_string = ""
  for char in string:
    if char not in "aeiou":
      new_string += "*"
    else:
      new_string += char
  return new_string

def replace_consonants(strings):
  for i in range(len(strings)):
    if strings[i] not in "aeiou":
      strings[i] = "*"
  return strings

if __name__ == "__main__":
  assert censor_consonants("abcd") == "a***"
  
  letters = ["a", "b", "c", "d"]
  replace_consonants(letters)
  assert letters == ["a", "*", "*", "*"]