Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Класс, реализующий расширение типов "встраиванием"
- """
- class Set:
- def __init__(self, value = []):
- self.data = []
- self.concat(value)
- def intersect(self, other):
- res = []
- for x in self.data:
- if x in other:
- res.append(x)
- return Set(res)
- def union(self, other):
- res = self.data[:]
- for x in other:
- if not x in res:
- res.append(x)
- return Set(res)
- def concat(self, value):
- for x in value:
- if not x in self.data:
- self.data.append(x)
- def __len__(self):
- return len(self.data)
- def __getitem__(self, key):
- return self.data[key]
- def __and__(self, other):
- return self.intersect(other)
- def __or__(self, other):
- return self.union(other)
- def __repr__(self):
- return 'Set:' + repr(self.data)
- """
- Подкласс, реализующий передачу методам & и | любого количества аргументов
- """
- class Sub(Set):
- def intersect(self, *others):
- result = []
- for arg in self.data:
- for other in others:
- if arg not in other: break
- else:
- result.append(arg)
- return Set(result)
- def union(self, *others):
- result = self.data[:]
- for value in others:
- for arg in value:
- if not arg in result:
- result.append(arg)
- return Set(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement