Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment you will program a few functions that allow a list to have the functionality of a set data structure ( remember list

In this assignment you will program a few functions that allow a list to have the functionality of a set data structure (remember list can have duplicates but sets do not so your code should not return 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 have also (mostly) implemented two of the functions to give you an idea of what's expected (set_new(), and set_remove()). All the functions should be just a few lines of code.In this assignment we will represent sets using the Python list data 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 be represented by a list that has the elements of the set in a different order. In your implementation, the result returned by your code should not depend on the order in which the elements appear 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 the user # has provided the right kind of input: if type(s)!=type([]) : raise ValueError # we can simply use the "remove" method of a list: s.remove(value) # to be complete before returning we should make sure there are no duplicates - not shown return sdef set_union(s1, s2) :"""Return the union of sets s1 and s2 as a list""" if type(s1)!=type([]) or type(s2)!=type([]): raise ValueError returndef set_intersection(s1, s2) :"""Return the intersection of sets s1 and s2 as a list""" if type(s1)!=type([]) or type(s2)!=type([]): raise ValueError returndef set_membership(s, value):"""Return True if value is in the set s, and False otherwise""" if type(s)!=type([]) : raise ValueError returndef set_equals(s1, s2) :"""Return True if the sets s1 and s2 have exactly the same elements""" if type(s1)!=type([]) or type(s2)!=type([]): raise ValueError returndef 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 returndef is_proper_subset(s1, s2) :"""Return True if s1 is a proper subset of s2""" if type(s1)!=type([]) or type(s2)!=type([]): raise ValueError return

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

What do you think of the MBO program developed by Drucker?

Answered: 1 week ago