Question: 2.8 Set Operations in Python This lab will be available until September 5th, 11:59 PM MDT In this assignment you will program a few functions

2.8 Set Operations in Python

This lab will be available until September 5th, 11:59 PM MDT

In this assignment you will program a few functions that allow alist to have the functionality of a set data structure (rememberlist can have duplicates but sets do not so your code should notreturn duplicate values even if the input list contain duplicates).This includes creating a new set, adding/removing elements etc.Complete details are provided in the code template below. We havealso (mostly) implemented two of the functions to give you an ideaof what's expected (set_new(), and set_remove()). All the functionsshould be just a few lines of code.

In this assignment we will represent sets using the Python listdata structure. For example:

s = [1,'a',3]

represents the mathematical set object {1, 'a', 3}.

Note that since order does not matter, the same set can berepresented by a list that has the elements of the set in adifferent order. In your implementation, the result returned byyour code should not depend on the order in which the elementsappear in the list.

Your job is to implement the following set operations:

def set_new() :
"""Return a new set"""
return []

def set_remove(s, value):
"""Remove the given value from the set s"""
# perform some type checking to see that theuser
# has provided the right kind of input:
if type(s)!=type([]) :
raise ValueError
# we can simply use the "remove" method of alist:
s.remove(value)
# to be complete before returning we should make surethere are no duplicates - not shown
return s

def set_union(s1, s2) :
"""Return the union of sets s1 and s2 as alist"""
if type(s1)!=type([]) or type(s2)!=type([]):
raise ValueError
return

def set_intersection(s1, s2) :
"""Return the intersection of sets s1 and s2 as alist"""
if type(s1)!=type([]) or type(s2)!=type([]):
raise ValueError
return

def set_membership(s, value):
"""Return True if value is in the set s, and Falseotherwise"""
if type(s)!=type([]) :
raise ValueError
return

def set_equals(s1, s2) :
"""Return True if the sets s1 and s2 have exactly thesame elements"""
if type(s1)!=type([]) or type(s2)!=type([]):
raise ValueError
return

def set_difference(s1, s2) :
"""Return the set difference of s1 and s2"""
if type(s1)!=type([]) or type(s2)!=type([]):
raise ValueError
return []

def is_subset(s1, s2) :
"""Return True if s1 is a subset of s2"""
if type(s1)!=type([]) or type(s2)!=type([]):
raise ValueError
return

def is_proper_subset(s1, s2) :
"""Return True if s1 is a proper subset ofs2"""
if type(s1)!=type([]) or type(s2)!=type([]):
raise ValueError
return

Step by Step Solution

3.39 Rating (149 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!