class Set: def __init__(self): self.my_set = MyList() def length(self): return self.my_set.__len__() def contains(self, item): return self.my_set.__contains__(item) def add(self, item): nd = ListNode(item) self.my_set.add(nd) def remove(self, item): self.my_set.remove(item) def iterator(self): return self.my_set.__iter__() def equals(self, other_set): if type(other_set) is not Set: print("Unsuitable object type!") return "" if self.length() != other_set.length(): return False set1 = other_set.iterator() for elem in set1: if not self.my_set.__contains__(elem): return False return True def difference(self, other_set): if type(other_set) is not Set: print("Unsuitable object type!") return "" result_set = Set() it = self.iterator() for item in it: if not other_set.contains(item): result_set.add(item) return result_set def issubsetof(self, other_set): if type(other_set) is not Set: print("Unsuitable object type!") return "" if self.length() > other_set.length(): return False for item in self.my_set: if not other_set.contains(item): return False return True
class MySet: def __init__(self): self.items = MyList() def length(self): return self.items.__len__() def contains(self, item): return self.items.__contains__(item) def isEmpty(self): return self.items.__len__() == 0 def add(self, item): i = ListNode(item) self.items.add(i) def remove(self, item): i = ListNode(item) return self.items.remove(i) def equals(self, setB): if self.length() != setB.length(): return False else: return self.isSubsetOf(setB) def isSubsetOf(self, setB): for element in self: if element not in setB: return False return True def union(self, setB): newSet = MySet() for element in self.items: newSet.add(element) for element in setB: if element not in self: newSet.add(element) return newSet def difference(self, setB): newSet = MySet() for element in self.items: newSet.add(element) for element in setB: if element in self: newSet.remove(element) return newSet def print_set(self): for i in self.items: print(i, end=', ') def __iter__(self): return self.items.__iter__()
class Set: def __init__(self): self.linkedList = MyList() def length(self): return abs(self.linkedList.__len__) def contains(self): return self.linkedList.__contains__ def add(self, item): newItem = ListNode(item) self.linkedList.add(newItem) def remove(self, target): self.linkedList.remove(target) def isSubsetOf(self, inputSet): for element in self.linkedList: if element not in inputSet.linkedList: return False return True def equals(self, inputSet): if len(self.linkedList) != len(inputSet.linkedList): return False else: return self.isSubsetOf(inputSet) def intersection(self, setB): newSet = Set() for element in setB.linkedList: if self.linkedList.__contains__(element): newSet.add(element) return newSet def __repr__(self): output = "Set: " newList = list() for i in self.linkedList: newList.append(i) output += str(newList) return output