2019-11-07 08:58:16 -05:00
|
|
|
class CustomSet:
|
2021-11-18 22:22:32 +01:00
|
|
|
def __init__(self, elements=None):
|
|
|
|
|
self.elements = list(elements) if elements is not None else list([])
|
2018-01-19 05:36:08 -06:00
|
|
|
|
|
|
|
|
def isempty(self):
|
|
|
|
|
return not self.elements
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
|
return iter(self.elements)
|
|
|
|
|
|
|
|
|
|
def __contains__(self, element):
|
|
|
|
|
return element in self.elements
|
|
|
|
|
|
|
|
|
|
def issubset(self, other):
|
2021-11-18 22:22:32 +01:00
|
|
|
return all(idx in other for idx in self)
|
2018-01-19 05:36:08 -06:00
|
|
|
|
|
|
|
|
def isdisjoint(self, other):
|
2021-11-18 22:22:32 +01:00
|
|
|
return all(idx not in other for idx in self)
|
2018-01-19 05:36:08 -06:00
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
|
return self.issubset(other) and other.issubset(self)
|
|
|
|
|
|
|
|
|
|
def add(self, element):
|
|
|
|
|
if element not in self:
|
|
|
|
|
self.elements.append(element)
|
|
|
|
|
|
|
|
|
|
def intersection(self, other):
|
|
|
|
|
result = CustomSet()
|
2021-11-18 22:22:32 +01:00
|
|
|
for idx in self:
|
|
|
|
|
if idx in other:
|
|
|
|
|
result.add(idx)
|
2018-01-19 05:36:08 -06:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def __sub__(self, other):
|
|
|
|
|
result = CustomSet()
|
2021-11-18 22:22:32 +01:00
|
|
|
for idx in self:
|
|
|
|
|
if idx not in other:
|
|
|
|
|
result.add(idx)
|
2018-01-19 05:36:08 -06:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
|
result = CustomSet(self.elements)
|
2021-11-18 22:22:32 +01:00
|
|
|
for idx in other:
|
|
|
|
|
result.add(idx)
|
2018-01-19 05:36:08 -06:00
|
|
|
return result
|