Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. class Set:
  2. def __init__(self,l=[]): #VERSION 1, doesn't use addElement or member
  3. """
  4. Takes in a list of object, stores that list in the same order it came
  5. with any duplicates removed.
  6. :param list: a list of objects
  7. :return:
  8. """
  9. self._elements=[]
  10. if l!=[] and isinstance(l,list):
  11. i=j=0#i index for list, j index for elements
  12. found_flag = False
  13. while i < len(l):
  14. while j < len(self._elements) and found_flag==False:
  15. if self._elements[j]==l[i]:
  16. found_flag #does this set to true?
  17. j+=1
  18. if found_flag == False:
  19. self._elements.append(l[i])
  20. found_flag #does this set to true?
  21. i+=1
  22. j=0
  23. def addElement(self,x):
  24. """
  25. If x is not in the Set object, it will be added
  26. :param x: object to be added
  27. :return:
  28. """
  29. if self._elements==[]:
  30. self._elements.append(x)
  31. else: #would be neater if you used member
  32. for i in self._elements:
  33. if i == x:
  34. return
  35. self._elements.append(x)
  36. def deleteElement(self,x):
  37. """
  38. Removes x from the Set object if present
  39. :param x: Object to be removed
  40. :return:
  41. """
  42. for i in self._elements:
  43. if i==x:
  44. self._elements.remove(i)
  45. return
  46. def member(self,x):
  47. """
  48. Returns True if x is in the Set object, else returns False
  49. :param x: Object to search for
  50. :return:
  51. """
  52. for i in self._elements:
  53. if i==x:
  54. return True
  55. return False
  56. def intersection(self,set2):
  57. """
  58. Returns a Set object containing objects common to self and set2
  59. :param set2: A second Set object
  60. :return: A set object with objects common to both self and set2
  61. """
  62. common=[]
  63. if isinstance(set2,Set) and self._elements!=[] and set2._elements!=[]:
  64. for i in self._elements:
  65. if set2.member(i):
  66. common.append(i)
  67. return Set(common)
  68. def subtract(self,set2):
  69. """
  70. Returns a new Set object containing all the elements in the calling set
  71. that are not in set2
  72. :param set2: A second Set object
  73. :return: A set object with objects from calling object that are not in set2
  74. """
  75. not_common=[]
  76. if not isinstance(set2,Set) or self._elements==[]:
  77. return Set(not_common)
  78. elif set2._elements==[]:
  79. return Set(self._elements)
  80. else:
  81. for i in self._elements:
  82. if not set2.member(i):
  83. not_common.append(i)
  84. return Set(not_common)
  85. def __add__(self,set2):
  86. """
  87. Returns a new Set object containing all the elements of calling Set and
  88. set2.
  89. :param set2: A second Set object
  90. :return: A set object containing all the objects found in calling Set and
  91. set2
  92. """
  93. l=self.intersection(set2)._elements
  94. l.extend(self.subtract(set2)._elements)
  95. return Set(l)
  96. def __str__(self):
  97. """
  98. Returns a string that represents the objects stored in the calling Set in
  99. set notation.
  100. """
  101. return_str_maker=[]
  102. for i in self._elements:
  103. return_str_maker.append(str(i))
  104. return_str=','.join(return_str_maker)
  105. return_str='{' + return_str +'}'
  106. return return_str
  107. class IntSet(Set):
  108. def __init__(self,l=[]):
  109. Set.__init__(self,l)
  110. if l!=[]:
  111. for i in l:
  112. if isinstance(i,int) and not self.member(i):
  113. self.addElement(i)
  114. def addElement(self,x):
  115. if not isinstance(x,int):
  116. raise ValueError(str(x)+ 'is not an int.')
  117. self._element.append(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement