Compsci 6, Safe Sets March 14

Name____________________   net-id _________       

Name____________________   net-id _________       

Name____________________   net-id _________       

Lists and Sets

In answering the questions here assume that list_a and list_b are lists of strings, for example (but don't answer assuming these lists in particular -- however, you can assuming the lists don't contain duplicates):
list_a = ["brown", "aqua", "maroon", "orange", "blue", "red"]
list_b = ["black", "blue", "magenta", "red"]

  1. Which of the following creates a list of all the elements in either list_a or in list_b, but with no duplicates (more than one is possible):

    1. [x for x in list_a if x in list_b]

    2. list(set(list_a) | set(list_b))

    3. NOTHING

  2. Which of the following creates a list of all the elements in both list_a and in list_b, but with no duplicates (more than one is possible):

    1. [x for x in list_a if x in list_b]

    2. list(set(list_a) & set(list_b))

    3. NOTHING

  3. Create a list of the strings that are in list_a but not in list_b -- which of the following do this (more than one is possible)?

    1. [x for x in list_a if not x in list_b]

    2. list(set(list_a) - set(list_b))

    3. [e for e in list_a if list_b.count(e) < 2]

    
    
    
    
    
  4. Which of the following is/are true for the Python data-type/collection set and false for the data-type/collection list in Python (more than one is possible)

    1. mutable

    2. can contain duplicates

    3. determining in for elements is extremely fast

  5. Which of the following are equivalent to s^t for sets s and t (more than one is possible)?

    1. (s-t) | (t-s)

    2. (s|t) - (s&t)

    3. (s|t|(s&t))

  6. A list cannot be an element in a set. What's an alternative that often helps in this situation?

    1. Use a tuple instead of the list (tuples can be in sets).

    2. Use a list instead of a set (lists can be in lists).

    3. Use a string instead of a list (strings can be in sets).