1
+ from powerSetCalc import calcPSetOf
2
+
3
+
4
+ class Set :
5
+ def __init__ (self , * elements , universe = []):
6
+ # Iteration variable for tracking iterations
7
+ self .current = 0
8
+
9
+ # If the user mistakenly enters a single list to contain all elements then that list is converted into an
10
+ # elements list
11
+ if len (elements ) == 1 and (type (elements [0 ]) == list or type (elements [0 ]) == tuple ):
12
+ elements = elements [0 ]
13
+
14
+ self .elements = elements
15
+ elements = self .duplicateRemoval ()
16
+ # universe of discourse
17
+ self .universe = universe
18
+ # Converts each iterable element in the set into a set object
19
+ self .elements = tuple (self .toSet (elements ))
20
+ self .isProduct = False
21
+
22
+ def __len__ (self ):
23
+ return len (self .elements )
24
+
25
+ def __iter__ (self ):
26
+ # This is strictly used for the beginning of iteration
27
+ return self
28
+
29
+ '''def __setitem__(self, index, newitem):
30
+ self.elements[index] = newitem
31
+ '''
32
+
33
+ def __getitem__ (self , index ):
34
+ # Used for brackets element accessing
35
+ return self .elements [index ]
36
+
37
+ def __next__ (self ):
38
+ # Accesses the next element during iteration
39
+ if self .current < len (self ):
40
+ c = self .current
41
+ self .current += 1
42
+ return self .elements [c ]
43
+ else :
44
+ self .current = 0
45
+ raise StopIteration
46
+
47
+ def __str__ (self ):
48
+ return str (self .__list__ ()).replace ("[" , '{' ).replace (']' ,'}' )
49
+
50
+ def __list__ (self ):
51
+ '''
52
+ Converts the current instance of the set object into a standard list (along with each of its set elements)
53
+ :return: A list of the elements the set
54
+ '''
55
+
56
+ return_list = []
57
+ for i in range (len (self .elements )):
58
+ return_list .append (0 )
59
+
60
+ for i in range (len (self .elements )):
61
+ mutable = False
62
+
63
+ # Checks if the current element is mutable
64
+ try :
65
+ test_dict = dict ()
66
+ test_dict [self .elements [i ]] = 1
67
+ except TypeError :
68
+ mutable = True
69
+ if isinstance (self .elements [i ], Set ):
70
+ mutable = True
71
+
72
+ if mutable :
73
+ # If the current element is mutable, then it's converted to a list
74
+ # notice that even set objects will call the current method to convert to a list until the primitive
75
+ # types are reached
76
+ return_list [i ] = list (self .elements [i ])
77
+ else :
78
+ # If an element is not mutable than it can be added to the return list as is
79
+ return_list [i ] = self .elements [i ]
80
+ return return_list
81
+
82
+ def __set__ (self ):
83
+ return set (self .__list__ ())
84
+
85
+ def __eq__ (self , other ):
86
+ return self .equal (other )
87
+
88
+ def __abs__ (self ):
89
+ return len (self )
90
+
91
+ def __add__ (self , other ):
92
+ return self .union (other )
93
+
94
+ def __sub__ (self , other ):
95
+ return self .setMinus (other )
96
+
97
+ def __and__ (self , other ):
98
+ return self .intersection (other )
99
+
100
+ def __mul__ (self , other ):
101
+ return self .cartesianProduct (other )
102
+
103
+ def union (self , setb ):
104
+ Set .count = 0
105
+ result = []
106
+
107
+ for x in self :
108
+ if x not in result :
109
+ result .append (x )
110
+ for x in setb :
111
+ if x not in result :
112
+ result .append (x )
113
+
114
+ return Set (result )
115
+
116
+ def setMinus (self , setb ):
117
+ result = []
118
+
119
+ for x in self :
120
+ if x not in setb :
121
+ result .append (x )
122
+ return Set (result )
123
+
124
+ def complement (self ):
125
+ return self .setMinus (self .universe )
126
+
127
+ def intersection (self , setb ):
128
+ result = []
129
+
130
+ for x in self :
131
+ if x in setb :
132
+ result .append (x )
133
+
134
+ return Set (result )
135
+
136
+ def toSet (self , elements ):
137
+ '''
138
+ A helper function used to convert a list of elements to a set; however the constructor should be called for this
139
+ purpose not this method
140
+ :param elements: A list of elements
141
+ :return: A list of elements where all mutable elements have been converted to set objects
142
+ '''
143
+
144
+ return_list = []
145
+
146
+ # Extends the return list to be the length of the number of elements submitted
147
+ for i in range (len (elements )):
148
+ return_list .append (0 )
149
+
150
+ for i in range (len (elements )):
151
+ mutable = False
152
+
153
+ # Checks to see if the current element is mutable
154
+ try :
155
+ testDict = dict ()
156
+ testDict [elements [i ]] = 1
157
+ except TypeError :
158
+ mutable = True
159
+
160
+ # The set class is not considered mutable
161
+ if isinstance (elements [i ], Set ):
162
+ mutable = False
163
+
164
+ if mutable :
165
+ # Converts the current element to a set object
166
+ return_list [i ] = Set (elements [i ], universe = self .universe )
167
+ else :
168
+ # If the current object is a base type, then it does not need to be converted and is kept as is
169
+ return_list [i ] = elements [i ]
170
+ return return_list
171
+
172
+ def duplicateRemoval (self ):
173
+ '''
174
+ Removes duplicate elements from the given set object (this is not intended to be called from outside the class
175
+ :return: A list of non-repeating elements
176
+ '''
177
+ clean = []
178
+ for element in self .elements :
179
+ if element not in clean :
180
+ clean .append (element )
181
+ return clean
182
+
183
+ def powerSet (self ):
184
+ '''
185
+ Calculates the power set of a set
186
+ :return: A power set set object
187
+ '''
188
+
189
+ _self = self .__list__ ()
190
+ powerSet = calcPSetOf (_self )
191
+ return Set (powerSet )
192
+
193
+
194
+ def subsetof (self , setb ):
195
+ '''
196
+ Checks if the current set is a subset of the input set
197
+ :param setb: comparison set
198
+ :return: Is this set is a subset of setb
199
+ '''
200
+
201
+ for y in self :
202
+ if y not in setb :
203
+ return False
204
+ return True
205
+
206
+ def equal (self , setb ):
207
+ '''
208
+ This set is equal to set b
209
+ :param setb:
210
+ :return:
211
+ '''
212
+
213
+ if self .subsetof (setb ) and setb .subsetof (self ):
214
+ return True
215
+ else :
216
+ return False
217
+
218
+ def setDisplayMode (self ):
219
+ '''
220
+ Displays each element of the current set by hitting enter each time. This is a good way to display a very long
221
+ set especially when copying
222
+ :return:
223
+ '''
224
+
225
+ for x in self :
226
+ print (x , end = "\n " )
227
+ input ()
228
+
229
+ def cartesianProduct (self , * setb ):
230
+ return Set (self .aCartesianProduct (setb ))
231
+
232
+
233
+ def aCartesianProduct (self , * setb ):
234
+ result = []
235
+ if type (setb [0 ]) == list or type (setb [0 ]) == tuple and len (setb ) == 1 :
236
+ setb = setb [0 ]
237
+
238
+ if len (setb ) == 1 :
239
+ for x in self :
240
+ for y in setb [0 ]:
241
+ result .append ((x ,y ))
242
+ else :
243
+ otherComb = setb [0 ].cartesianProduct (setb [1 :])
244
+ for i in range (len (self )):
245
+ for y in otherComb :
246
+ _y = list (y )
247
+ _y .insert (0 , self [i ])
248
+ result .append (tuple (_y ))
249
+ return result
250
+
251
+
252
+
253
+
254
+
255
+ A = Set (1 ,[[2 ,3 ]])
256
+ print (list (A ))
0 commit comments