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

write the following four Python functions: to_set: This function accepts a list

ID: 3929573 • Letter: W

Question

write the following four Python functions:

to_set: This function accepts a list and returns a modified version of that list that is a set. Examples:

union: This function accepts two sets (represented as lists) and returns their set union. Example:

Since ordering in a set doesn't matter, if your code produced the set [3,4,2,1] in the above example, that would be OK.

intersect: This function accepts two sets (represented as lists) and returns their set intersection. Example:

symmetric_diff: This function takes two sets (again, represented as lists) and produces the symmetric difference. Example:

Explanation / Answer

def to_set(li):
   ans=[]
   for a in li:
       if a not in ans:
           ans.append(a)
   return ans
def union(li1,li2):
   ans=[]
   for a in li1:
       if a not in ans:
           ans.append(a)
   for a in li2:
       if a not in ans:
           ans.append(a)
   return ans
def intersect(li1,li2):
   ans=[]
   for a in li1:
       if a in li2:
           ans.append(a)
   return ans
def symmetric_diff(li1,li2):
   ans=[]
   for a in li1:
       if a not in li2:
           ans.append(a)
   for a in li2:
       if a not in li1:
           ans.append(a)
   return ans