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

Write a class called Set that implements a set object similar to the builtin set

ID: 3891895 • Letter: W

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 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}

Explanation / Answer

class Set:
   def __init__(self,l):
     self.list = []
     for i in range(len(l)):
         if l[i] not in self.list:
            self.list.append(l[i])

   def __str__(self):
      str1 = "["
      for i in range(len(self.list)):
          if i < len(self.list) - 1:
             str1 = str1 + str(self.list[i]) + ","
          else:
             str1 = str1 + str(self.list[i])
      str1 = str1 + "]"
      return str1

   def __add__(self,other):
       list1 = []
       for i in range(len(self.list)):
           list1.append(self.list[i])
       for i in range(len(other.list)):
          if other.list[i] not in list1 :
             list1.append(other.list[i])
       #print(self.list)
       return Set(list1)

   def __sub__(self,other):
       list1 = []
       for i in range(len(self.list)):
           if self.list[i] not in other.list:
              list1.append(self.list[i])
       return Set(list1)

   def __mul__(self,other):
       list1 = []
       for i in range(len(self.list)):
           if self.list[i] in other.list:
              list1.append(self.list[i])
       return Set(list1)

   def __contains__(self,elem):
       if elem in self.list:
          return True
       else:
          return False

   def insert(self,x):
       if x not in self.list:
          self.list.append(x)

   def to_list(self):
       list1 = []
       for i in range(len(self.list)):
          list1.append(self.list[i])
       return list1


a = Set([0,1,2,1,0,3,3])
b = Set([2,3,4,5])

print(a)
u = a + b
print(u)
i = a * b
print(i)
d = a - b
#print(d)
print(3 in b)
print(0 in b)

print(u.to_list())
print(a * Set([10,20]))
a.insert("new object")
print(a)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote