write the following four Python functions toset This functio
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:
Solution
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
