File: LP6E/Chapter32/setwrapper.py
class Set:
def __init__(self, value = []): # Constructor
self.data = [] # Manages a list
self.concat(value) # Removes duplicates
def intersect(self, other): # other is any iterable
res = [] # self is the subject
for x in self.data:
if x in other: # Pick common items
res.append(x)
return Set(res) # Return a new Set
def union(self, other): # other is any iterable
res = self.data[:] # Copy of my list
for x in other: # Add items in other
if not x in res:
res.append(x)
return Set(res)
def concat(self, value): # value: list, Set...
for x in value: # Removes duplicates
if not x in self.data:
self.data.append(x)
def __len__(self): return len(self.data) # len(self), if self
def __getitem__(self, key): return self.data[key] # self[i], self[i:j]
def __and__(self, other): return self.intersect(other) # self & other
def __or__(self, other): return self.union(other) # self | other
def __repr__(self): return f'Set({self.data!r})' # print(self),...
def __iter__(self): return iter(self.data) # for x in self,...