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

Create a file named Set.py with a class named Set that implements a class for re

ID: 3785841 • Letter: C

Question

Create a file named Set.py with a class named Set that implements a class for representing a mathematical set (an unordered collection of distinct items). Also implement a unit test file named test_Set.py that tests each of your methods. Note, with the except of the__init__and insert method, none of the other methods should mutate the set itself or any of the parameters. Implement methods named: insert-takes one parameter which is the item to insert into the set contains-takes one parameter and returns True or False indicating if the parameter is in the set isSubsetOf-takes one parameter that is a Set object and returns True if the set is a subset of the parameter set and false otherwise __len__- returns the number of items in the set implement the method for the + operator that returns a new Set that is the union of the two sets implement the method for the-operator that returns a new Set which is the difference of the two sets (the items in the left set that are not in the right set) implement the methods for the = = and ! = methods that return True or false depending on the two sets and the corresponding operator Implement your Set using one instance variable - a list of the items that are in the set. Each method will use the methods/operators that lists have to implement the set method. Write appropriate documentation strings that describe what each Sot method docs. Also comment the

Explanation / Answer

Set.py :

class Set:
def __init__(self, *args):
self._dict = {}
for arg in args:
self.add(arg)

def extend(self, args):
""" Add several items at once. """
for arg in args:
self.add(arg)
  

def add(self, item):
""" Add one item to the set. """
self._dict[item] = item
      

def remove(self, item):
""" Remove an item from the set. """
del self._dict[item]
  
def printList(self):
for item in self:
print(item)

def contains(self, item):
""" Check whether the set contains a certain item. """
for i in self:
if i == item:
return 'true'
return 'false'
  
  
  
def isSubsetOf(s1,s2):
for item in s2:
if s1.contains(item) == 'false':
return 'false'
return 'true'

def __getitem__(self, index):
""" Support the 'for item in set:' protocol. """
return self._dict.keys( )[index]

def __iter__(self):
""" Better support of 'for item in set:' via Python 2.2 iterators """
return iter(self._dict.copy( ))

def __len__(self):
""" Return the number of items in the set """
return len(self._dict)

def __copy__(self):
return Set(self)
  
def union(s1, s2):
import copy
result = copy.copy(s1)
for item in s2:
result.add(item)
return result
  
def __add__(s1,s2):
result = s1
for item in s2:
result.add(item)
return result
  
  
def __sub__(s1,s2):
result=s1;
for item in s2:
if s1.contains(item):
result.remove(item)
return result
  
def __eq__(s1,s2):
for items in s1:
if s2.contains(items) == 'false':
return False
return True
  
  
def __hash__(self):
return hash(self._dict)
  
  
def __ne__(s1,s2):
for items in s1:
if s2.contains(items) == 'true':
return True
return False

main.py

# Hello World program in Python
from Set import *
  
s1 = Set(4,3)

s1.add(1)
s1.add(2)

s2= Set(4,3)
print("Set s1: ")
s1.printList()
print("Set s2: ")
s2.printList()
if s1.isSubsetOf(s2) == 'true':
print("subset")
else:
print("no subset")
  
s=s1+s2
print("Addition of Sets : ")
s.printList()

s=s1-s2
print("Intersection of Sets : ")
s.printList()

print("equality :")
if s1==s2:
print("s1 == s2")

if s1!= s2:
print("s1 !=s2")


  

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