I am completely stuck and am looking for guidance. I am creating my own version of the sets class, making the class called Set. I have created a test function as well. Everything works great until I get to the union/subtract part.
Now, intersection works fine. I basically was following the same pattern for union, but when I run it...
Thoughts? I've messed around with this quite a bit, trying to just copy self.elements and then verbatim use the intersection() code with the exception of making the if statement i!=j. It just does not work.
Code:
def intersection(self,set2):
self.inter=Set([])
for i in self.elements:
for j in set2.elements:
if i == j:
self.inter = self.inter.addElement(j)
return self.inter
def union(self,set2):
self.un=Set([])
for i in self.elements:
self.un = self.un.addElement(i)
for j in set2.elements:
if i!=j:
self.un = self.un.addElement(j)
return self.un
def subtract(self,set2):
self.sub=Set([])
for i in self.elements:
self.sub= self.sub.addElement(i)
for j in set2.elements:
if i==j:
self.sub= self.sub.deleteElement(i)
return self.sub
Code:
Traceback (most recent call last):
File "C:\Users\monty27\Desktop\Math Computing\setsMontgomery.py", line 87, in <module>
main()
File "C:\Users\monty27\Desktop\Math Computing\setsMontgomery.py", line 83, in main
print "Union:", set1.union(set2)
File "C:\Users\monty27\Desktop\Math Computing\setsMontgomery.py", line 39, in union
self.un = self.un.addElement(j)
AttributeError: 'list' object has no attribute 'addElement'
Comment