fork download
  1. class Set:
  2.  
  3. set = []
  4.  
  5. def __init__(self,Thelist):
  6. self.set = Thelist
  7.  
  8. def __sub__(self,other):
  9. out = []
  10. for el in self.set:
  11. found = False
  12. for el2 in other.set:
  13. if el == el2:
  14. found = True
  15. break
  16. if found is False:
  17. out.append(el)
  18. return out
  19.  
  20. def __add__(self,other):
  21. out = []
  22. for i in other.set:
  23. out.append(i)
  24. for el in self.set:
  25. found = False
  26. for el2 in other.set:
  27. if el == el2:
  28. found = True
  29. break
  30. if found is False:
  31. out.append(el)
  32. out.sort()
  33. return out
  34.  
  35.  
  36.  
  37. def __and__(self,other):
  38. out = []
  39. for el in self.set:
  40. found = False
  41. for el2 in other.set:
  42. if el == el2:
  43. found = True
  44. break
  45. if found is True:
  46. out.append(el)
  47. return out
  48.  
  49. def fn():
  50. list1 = [1,2,3,4,5,6,7,102]
  51. print(list1)
  52. list2 = [10,101,2,3,102,202]
  53. list2.sort()
  54. print(list2)
  55. set1 = Set(list1)
  56. set2 = Set(list2)
  57. print("Intersect: ",set1&set2)
  58. print("Union",set1+set2)
  59. print("Diff",set1-set2)
  60. fn()
Success #stdin #stdout 0.03s 9660KB
stdin
Standard input is empty
stdout
[1, 2, 3, 4, 5, 6, 7, 102]
[2, 3, 10, 101, 102, 202]
Intersect:  [2, 3, 102]
Union [1, 2, 3, 4, 5, 6, 7, 10, 101, 102, 202]
Diff [1, 4, 5, 6, 7]