fork download
  1. class Set:
  2.  
  3. arr = []
  4.  
  5. def __init__(self, lista):
  6.  
  7. self.arr = lista
  8.  
  9. def __xor__(self, other):
  10.  
  11. r1 = self - other
  12. r2 = other - self
  13. return r1 + r2
  14.  
  15. def __and__(self, other):
  16.  
  17. r = []
  18. for i in self.arr:
  19. found = False
  20. for j in other.arr:
  21. if i == j:
  22. found = True
  23. if found is True:
  24. r.append(i)
  25. return r
  26.  
  27. def __add__(self, other):
  28.  
  29. r = []
  30. for i in other.arr:
  31. r.append(i)
  32.  
  33. for i in self.arr:
  34. found = False
  35. for j in other.arr:
  36. if i == j:
  37. found = True
  38. if found is False:
  39. r.append(i)
  40. return r
  41.  
  42. def __sub__(self, other):
  43.  
  44. r = []
  45. for i in self.arr:
  46. found = False
  47. for j in other.arr:
  48. if i == j:
  49. found = True
  50. if found is False:
  51. r.append(i)
  52. return r
  53.  
  54. def contains(self, x):
  55. found = False
  56. for i in self.arr:
  57. if i == x:
  58. found = True
  59. return found
  60.  
  61. def __str__(self):
  62. return str(self.arr)
  63.  
  64. ob1 = Set([0,1,2,3,4,5,-1])
  65. ob2 = Set([5,6,7,8,9,0])
  66. ob3 = ob1 - ob2
  67. ob33 = ob2 - ob1
  68. ob4 = ob1 + ob2
  69. ob5 = ob1 & ob2
  70. ob6 = ob1 ^ ob2
  71. print("A = ", ob1)
  72. print("B = ", ob2)
  73. print("Difference A - B: ", ob3)
  74. print("Difference B - A: ", ob33)
  75. print("Union:", ob4)
  76. print("Intersection: ", ob5)
  77. print(ob2.contains(1))
  78. print("Symmetric Diff: ", ob6)
  79. a = Set(['apple','orange','banana'])
  80. b = Set(['pink','banana','yellow','white'])
  81. n = b - a
  82. print(n)
  83.  
Success #stdin #stdout 0.03s 9084KB
stdin
Standard input is empty
stdout
A =  [0, 1, 2, 3, 4, 5, -1]
B =  [5, 6, 7, 8, 9, 0]
Difference A - B:  [1, 2, 3, 4, -1]
Difference B - A:  [6, 7, 8, 9]
Union: [5, 6, 7, 8, 9, 0, 1, 2, 3, 4, -1]
Intersection:  [0, 5]
False
Symmetric Diff:  [1, 2, 3, 4, -1, 6, 7, 8, 9]
['pink', 'yellow', 'white']