| 1 | 1 | class CustomSet: |
| 2 | 2 | def __init__(self, elements=[]): |
| 3 | | pass |
| 4 | | |
| 3 | # Edge Case: Avoid mutable default argument trap |
| 4 | if elements is None: |
| 5 | elements = [] |
| 6 | self.elements = [] |
| 7 | for element in elements: |
| 8 | self.add(element) |
| 9 | |
| 5 | 10 | def isempty(self): |
| 6 | | pass |
| 7 | | |
| 11 | return len(self.elements) == 0 |
| 12 | |
| 8 | 13 | def __contains__(self, element): |
| 9 | | pass |
| 10 | | |
| 14 | return element in self.elements |
| 15 | |
| 11 | 16 | def issubset(self, other): |
| 12 | | pass |
| 13 | | |
| 17 | # Edge Case: Handle when other is not a CustomSet |
| 18 | if not isinstance(other, CustomSet): |
| 19 | return NotImplemented |
| 20 | # Edge Case: Empty set is subset of any set |
| 21 | if self.isempty(): |
| 22 | return True |
| 23 | # Check if all elements in self are in other |
| 24 | for element in self.elements: |
| 25 | if element not in other: |
| 26 | return False |
| 27 | return True |
| 28 | |
| 14 | 29 | def isdisjoint(self, other): |
| 15 | | pass |
| 16 | | |
| 30 | # Edge Case: Handle when other is not a CustomSet |
| 31 | if not isinstance(other, CustomSet): |
| 32 | return NotImplemented |
| 33 | # Edge Case: Empty sets are disjoint |
| 34 | if self.isempty() or other.isempty(): |
| 35 | return True |
| 36 | # Check if there are no common elements |
| 37 | for element in self.elements: |
| 38 | if element in other: |
| 39 | return False |
| 40 | return True |
| 41 | |
| 17 | 42 | def __eq__(self, other): |
| 18 | | pass |
| 19 | | |
| 43 | # Edge Case: Handle when other is not a CustomSet |
| 44 | if not isinstance(other, CustomSet): |
| 45 | return NotImplemented |
| 46 | # Edge Case: Both empty sets are equal |
| 47 | if self.isempty() and other.isempty(): |
| 48 | return True |
| 49 | # Edge Case: Different sizes means not equal |
| 50 | if len(self.elements) != len(other.elements): |
| 51 | return False |
| 52 | # Check if all elements match (order doesn't matter) |
| 53 | for element in self.elements: |
| 54 | if element not in other: |
| 55 | return False |
| 56 | return True |
| 57 | |
| 20 | 58 | def add(self, element): |
| 21 | | pass |
| 22 | | |
| 59 | if element not in self.elements: |
| 60 | self.elements.append(element) |
| 61 | |
| 23 | 62 | def intersection(self, other): |
| 24 | | pass |
| 25 | | |
| 63 | # Edge Case: Handle when other is not a CustomSet |
| 64 | if not isinstance(other, CustomSet): |
| 65 | return NotImplemented |
| 66 | # Edge Case: Intersection with empty set is empty |
| 67 | if self.isempty() or other.isempty(): |
| 68 | return CustomSet() |
| 69 | # Create new set with common elements |
| 70 | result_elements = [] |
| 71 | for element in self.elements: |
| 72 | if element in other: |
| 73 | result_elements.append(element) |
| 74 | return CustomSet(result_elements) |
| 75 | |
| 26 | 76 | def __sub__(self, other): |
| 27 | | pass |
| 28 | | |
| 77 | # Edge Case: Handle when other is not a CustomSet |
| 78 | if not isinstance(other, CustomSet): |
| 79 | return NotImplemented |
| 80 | # Edge Case: Subtracting from empty set results in empty set |
| 81 | if self.isempty(): |
| 82 | return CustomSet() |
| 83 | # Edge Case: Subtracting empty set leaves original set |
| 84 | if other.isempty(): |
| 85 | return CustomSet(self.elements) |
| 86 | # Create new set with elements in self but not in other |
| 87 | result_elements = [] |
| 88 | for element in self.elements: |
| 89 | if element not in other: |
| 90 | result_elements.append(element) |
| 91 | return CustomSet(result_elements) |
| 92 | |
| 29 | 93 | def __add__(self, other): |
| 30 | | pass |
| 94 | # Edge Case: Handle when other is not a CustomSet |
| 95 | if not isinstance(other, CustomSet): |
| 96 | return NotImplemented |
| 97 | # Edge Case: Union with empty set is the other set |
| 98 | if self.isempty(): |
| 99 | return CustomSet(other.elements) |
| 100 | if other.isempty(): |
| 101 | return CustomSet(self.elements) |
| 102 | # Create new set with all unique elements from both sets |
| 103 | result_elements = list(self.elements) |
| 104 | for element in other.elements: |
| 105 | if element not in result_elements: |
| 106 | result_elements.append(element) |
| 107 | return CustomSet(result_elements) |
| 108 | |
| 109 | # Handled Edge Cases: Mutable default argument trap, non-CustomSet operands, empty sets in all operations, subset relationships, equality with different orders, disjoint sets, set differences, unions |