fork download
  1. class Set(list):
  2. def __init__(self, value = []):
  3. list.__init__([])
  4. self.concat(value)
  5.  
  6. def intersect(self, other):
  7. res = []
  8. for x in self:
  9. if x in other:
  10. res.append(x)
  11. return Set(res)
  12.  
  13. def union(self, other):
  14. res = Set(self)
  15. res.concat(other)
  16. return res
  17.  
  18. def concat(self, value):
  19. for x in value:
  20. if not x in self:
  21. self.append(x)
  22.  
  23. def __and__(self, other): return self.intersect(other)
  24. def __or__(self, other): return self.union(other)
  25. def __repr__(self): return 'Set:' + list.__repr__(self)
  26.  
  27. if __name__ == '__main__':
  28. x = Set([1,3,5,7])
  29. y = Set([2,1,4,5,6])
  30. print(x, y, len(x))
  31. print(x.intersect(y), y.union(x))
  32. print(x & y, x | y)
  33. x.reverse(); print(x)
Success #stdin #stdout 0.02s 28376KB
stdin
Standard input is empty
stdout
Set:[1, 3, 5, 7] Set:[2, 1, 4, 5, 6] 4
Set:[1, 5] Set:[2, 1, 4, 5, 6, 3, 7]
Set:[1, 5] Set:[1, 3, 5, 7, 2, 4, 6]
Set:[7, 5, 3, 1]