fork download
  1. class Set:
  2. def __init__(self, n):
  3. self.n = n
  4. self.set = [0]*(1000)
  5. def read():
  6. n = int(input("n="))
  7. A = Set(n)
  8. for i in range(0, n):
  9. A.set[i] = input("elem=")
  10. return A
  11.  
  12. def printf(set):
  13. for i in range(0,set.n-1):
  14. print(set.set[i], end = ", ")
  15. print(set.set[set.n-1])
  16.  
  17. def belong(set, elem):
  18. ok = False
  19. for i in range(0, set.n):
  20. if elem == set.set[i]:
  21. ok = True;
  22. break;
  23. return ok
  24.  
  25.  
  26. def main():
  27. A = read()
  28. print("Set A:")
  29. printf(A)
  30. B = read()
  31. print("Set B:")
  32. printf(B)
  33. I = []
  34. D = []
  35. R = B
  36. for index in range(0, A.n):
  37. if belong(B, A.set[index]) is True:
  38. I.append(A.set[index])
  39. else:
  40. D.append(A.set[index])
  41. R.set[R.n] = A.set[index]
  42. R.n =R.n + 1
  43. print("Intersection:")
  44. print(I)
  45. print("Difference")
  46. print(D)
  47. print("Union:")
  48. printf(R)
  49. main()
Success #stdin #stdout 0.03s 9740KB
stdin
5
1
2
3
4
5
3
-1
2
3
stdout
n=elem=elem=elem=elem=elem=Set A:
1, 2, 3, 4, 5
n=elem=elem=elem=Set B:
-1, 2, 3
Intersection:
['2', '3']
Difference
['1', '4', '5']
Union:
-1, 2, 3, 1, 4, 5