| 1 | 1 | class CustomSet: |
| 2 | 2 | def __init__(self, elements: list = []): |
| 3 | | pass |
| 3 | # Edge Case: Handle None input by treating it as an empty list |
| 4 | if elements is None: |
| 5 | elements = [] |
| 6 | # Edge Case: Remove duplicates by converting to set and back to list |
| 7 | self.elements = [] |
| 8 | for element in elements: |
| 9 | if element not in self.elements: |
| 10 | self.elements.append(element) |
| 4 | 11 | |
| 5 | 12 | def isempty(self) -> bool: |
| 6 | | pass |
| 13 | # Edge Case: Empty set check |
| 14 | return len(self.elements) == 0 |
| 7 | 15 | |
| 8 | 16 | def __contains__(self, element) -> bool: |
| 9 | | pass |
| 17 | # Edge Case: Check if element exists in the set |
| 18 | return element in self.elements |
| 10 | 19 | |
| 11 | 20 | def issubset(self, other: 'CustomSet') -> bool: |
| 12 | | pass |
| 21 | # Edge Case: Empty set is subset of any set |
| 22 | if self.isempty(): |
| 23 | return True |
| 24 | # Edge Case: Check if all elements of self are in other |
| 25 | for element in self.elements: |
| 26 | if element not in other.elements: |
| 27 | return False |
| 28 | return True |
| 13 | 29 | |
| 14 | 30 | def isdisjoint(self, other: 'CustomSet') -> bool: |
| 15 | | pass |
| 31 | # Edge Case: Empty sets are disjoint |
| 32 | if self.isempty() or other.isempty(): |
| 33 | return True |
| 34 | # Edge Case: Check if no elements are common between sets |
| 35 | for element in self.elements: |
| 36 | if element in other.elements: |
| 37 | return False |
| 38 | return True |
| 16 | 39 | |
| 17 | 40 | def __eq__(self, other: 'CustomSet') -> bool: |
| 18 | | pass |
| 41 | # Edge Case: Compare sets of different sizes |
| 42 | if len(self.elements) != len(other.elements): |
| 43 | return False |
| 44 | # Edge Case: Check if all elements match |
| 45 | for element in self.elements: |
| 46 | if element not in other.elements: |
| 47 | return False |
| 48 | return True |
| 19 | 49 | |
| 20 | 50 | def add(self, element) -> None: |
| 21 | | pass |
| 51 | # Edge Case: Avoid adding duplicate elements |
| 52 | if element not in self.elements: |
| 53 | self.elements.append(element) |
| 22 | 54 | |
| 23 | 55 | def intersection(self, other: 'CustomSet') -> 'CustomSet': |
| 24 | | pass |
| 56 | # Edge Case: Intersection with empty set results in empty set |
| 57 | result_elements = [] |
| 58 | # Edge Case: Find common elements between sets |
| 59 | for element in self.elements: |
| 60 | if element in other.elements: |
| 61 | result_elements.append(element) |
| 62 | return CustomSet(result_elements) |
| 25 | 63 | |
| 26 | 64 | def __sub__(self, other: 'CustomSet') -> 'CustomSet': |
| 27 | | pass |
| 65 | # Edge Case: Difference with empty set is the original set |
| 66 | result_elements = [] |
| 67 | # Edge Case: Find elements in self but not in other |
| 68 | for element in self.elements: |
| 69 | if element not in other.elements: |
| 70 | result_elements.append(element) |
| 71 | return CustomSet(result_elements) |
| 28 | 72 | |
| 29 | 73 | def __add__(self, other: 'CustomSet') -> 'CustomSet': |
| 30 | | pass |
| 74 | # Edge Case: Union with empty set is the other set |
| 75 | result_elements = self.elements[:] |
| 76 | # Edge Case: Add elements from other set that are not already in self |
| 77 | for element in other.elements: |
| 78 | if element not in result_elements: |
| 79 | result_elements.append(element) |
| 80 | return CustomSet(result_elements) |
| 81 | |
| 82 | # Handled Edge Cases: Empty set operations, duplicate element insertion, set comparison with different sizes, intersection/union with empty sets, subset checks with empty sets, disjoint checks with empty sets |