Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python A DictSet models a mathematical set. It\'s represented as a Python dictio

ID: 3931003 • Letter: P

Question

Python

A DictSet models a mathematical set. It's represented as a Python dictionary. If an element e is a member of the set, the dictionary contains the value True for the key and e; Otherwise the key e does not appear in the dictionary.

Recall that:

dd = {} is a new dictionary

dd.keys() gives us the keys in dd

dd[x] = "pie" sets the key in x to the string "pie" in the dictinoary dd

Complete this Python function:

# DictSet DictSet -> DictSet

Set intersection on DictSets

def set_intersect(set_a, set_b):

Explanation / Answer

dict_a = {1:"a",2:"b",3:"c",4:"d"}
dict_b = {2:"b",5:"e",4:"d",6:"g"}
set_a = dict_a.keys()
set_b = dict_b.keys()
def set_intersect(set_a, set_b):
   set_c = []
   for i in set_a:
       # add to set_c if set_a element in set_b
       if((i in set_b) and (i not in set_c)):
           set_c.append(i)
   return set_c
print set_intersect(set_a,set_b)

"""
sample output

[2, 4]
"""