Review: Sets

Module 2: Data Structures

Review of Session 2.2: Sets

Exercise

After running this cell, what will inside of st_names?

ls_names = ["Alberto", "Carol"]
tp_names = ("Alberto", "Lucia", "Ramon")
st_names = set(ls_names + list(tp_names))

print(st_names)
Exercise

After running this cell, what will be the length of st_movies?

st_comedy = {"Airplane!", "Shrek"}
st_fantasy = {"Harry Potter", "Shrek", "The Lord of the Rings"}
st_movies = st_comedy | st_fantasy  # union

print(len(st_movies))
Exercise

After running this cell, what will be the length of st_movies?

st_comedy = {"Airplane!", "Shrek"}
st_fantasy = {"Harry Potter", "Shrek", "The Lord of the Rings"}
st_movies = st_comedy and st_fantasy  # intersection

print(len(st_movies))

Remember and does not work for sets! Only &.

Exercise

After running this cell, which two structures will have the same length?

msg = "A B C"

ls_evens = [2, 2, 4, 6]

tp_pairs = ((1, 3), (3, 5), (5, 7))

st_squares = {4, 4, 9, 16}

print(len(msg), len(ls_evens), len(tp_pairs), len(st_squares))
Exercise

The following blocks all fail except one. Which one runs fine?

# Option A
ls_numbers = [1, 2, 3]
ls_numbers[0] = "0"

# Option B
ls_numbers = [1, 2, 3]
ls_numbers = ls_numbers + 4

# Option C
tp_numbers = tuple([1, 2, 3])
tp_numbers[0] = 0

# Option D
st_numbers = set([1, 2, 3])
st_numbers[0] = 2

Answer.

  1. Runs fine. A list can contain ANY kind of element, and mix them!

  2. Fails. You cannot sum a list and a number. We can add 4 to the list by enclosing it in brackets: ls_numbers + [4]

  3. Fails. Tuples are not mutable. They do not allow to change their elements! You can access the element tp_numbers[0] but not change it!

  4. Fails. Sets are not ordered. You cannot index them! st_numbers[0] causes an error.

Exercise

Three friends are choosing a weekend plan. Each friend has a list of plans they want to do together. Write a function that takes these three lists and finds the plan that all three friends like.

laura = ["beach", "cinema", "mountain", "museum", "shopping"]
jose = ["gym",  "mountain", "park", "soccer", "shopping"]
ana = ["beach", "cinema", "mountain", "park", "party"]
# This code solves the problem
laura = set(laura)
jose = set(jose)
ana = set(ana)

intersect = laura & jose & ana
print(list(intersect)[0])
# We can encapsulate the previous cell into a function
# This way, we can reuse it for any other group of friends!

def find_common_places(list1, list2, list3):
  set1 = set(list1)
  set2 = set(list2)
  set3 = set(list3)
  common_places = set1.intersection(set2, set3)
  return common_places

common_places = find_common_places(laura, jose, ana)
print(common_places)
Exercise

Based on the previous example, write a function that identifies which places are liked exclusively by each friend. This means the function should print, for each person, the places that only they like and no one else does.

def find_exclusive_places(list1, list2, list3):
  set1 = set(list1)
  set2 = set(list2)
  set3 = set(list3)
  exclusive1 = set1 - set2 - set3
  print(f"The first person likes {exclusive1} exclusively.")
  exclusive2 = set2 - set1 - set3
  print(f"The second person likes {exclusive2} exclusively.")
  exclusive3 = set3 - set1 - set2
  print(f"The third person likes {exclusive3} exclusively.")

find_exclusive_places(laura, jose, ana)
Exercise

Consider the following code. What will be the expected output?

st_names = {"Raul", "Ana", "Oscar"}

print(st_names)
Exercise

Consider the following code. What will be the expected output?

ls_names = ["Raul", "Ana", "Oscar"]
st_names = set(ls_names)

print(st_names[0])
Exercise

Consider the following code. What will be the expected output?

st1 = {"DOG", "CAT", "MOUSE"}
st2 = {"dog", "cat", "mouse"}

print(st1.intersection(st2))
# Lowercase the set
st2 = {"dog", "cat", "mouse"}

# Important: How to initalize a set
st_lower = set()

for element in st2:
  st_lower.add(element.lower())

print(st_lower)
Exercise

Consider the following code. What will be the expected output?

st1 = {1, 2, 3}
st2 = {1.0, 2.5, 3.0}

print(st1.union(st2))
Exercise

Consider the following code. What will be the expected output?

st1 = {1, 2, 3}
st2 = {"1", "2", "3"}

print(st1.difference(st2))
Exercise

Consider the following code. What will be the expected output?

st1 = {1, 1, 2, 2, 3, 3}
st2 = set([5, 4])

print(st1.union(st2))