Question: Write a class called Set that implements a set object similar to the builtin set class we studied. A Set object is a collection of

Write a class called Set that implements a set object similar to the builtin set class we studied. A Set object is a collection of objects with the semantics of a mathematical set: objects stored in a Set have no duplicates, i.e. adding an object x to a Set should check first if x already exists in the set using the equality operator and should add x to the set only if x did no belong there previously. The order of the elements in a Set is arbitrary. The Set class must have the following "public" methods: constructor, taking a parameter that is a list. The new Set object will contain the elements from the list, excluding duplicates. If the list is empty () then a new Set object is created with no elements. .str _(self): returns a string representation of the set that includes all elements' string representation. Set([0,1,2). str_0 should return "[0,1,2", with the order of elements being arbitrary ._add__(self, other): implements the union set operation and returns a new Set object with all elements of this (self) object and of the other set, excluding duplicates. sub_(self, other): implements the set difference operation and returns a new Set object with all elements of this (self) object that do not belong to the other set. mul_(self, other): implements the set intersection operation and returns a new Set object with all elements of this (self) object that also belong to the other set. -contains-(self, elem): returns True if the object elem belong to the current (self) set and False otherwise. This standard function is called by the in operator, as in: in A set insert(self, x): inserts object x to the current set (self) if x was not already in the set. . x to list(self): returns a new list object with the elements from the current (self) set object. Implementation requirements: class Set should not use in any way the standard classes set and dict to implement its methods or for storing the elements. The elements must be stored in a list object. Here are some examples how the Set class could be used: a -set([e, 1,2,1,0,3,3]) # passing a list with some duplicated elements b - Set ([2,3,4,5]) print(a) # displays {8,1,2,3} # Set u has elements (0,1,2,3,4,5) # Set i has elements {2,3} # Set d has elements {0,1} # displays True # displays False # displays [8,1,2,3,4,5] # displays {} print(3 in b) print (e in b) print(u.to_list)) print(a * Set([le, 201)) a.insert("new object") print(a) -- the empty set # displays {0, 1, 2, 3, new object}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
