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. -
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
-
Now, intersection works fine. I basically was following the same pattern for union, but when I run it... -
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'
-
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.
1 1940
Hi
This may or may not help, but the logic is wrong for intersection. You're adding all the elements of set2 multiple times. Basically for each i you add all the elements of set2 that are not equal to i (which is all of them or all of them except 1).
This is I suppose an issue since you appear to be going to quite a bit of effort to ensure that addElement only adds new elements.
If this is indeed the issue, I would consider making addElement itself more robust. So when you add an element, check if it's in there already and only add it if not.
Let me know if that helps. If not, please post the addElement method.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Amy Kimber |
last post by:
Hello,
I have a file upload page, and I've had it working fine, it was
beautiful :-) Anyway, the powers that be moved hosts... and it doesn't
work now.
The file name is correct, the directory...
|
by: M.N.A.Smadi |
last post by:
HI;
I am having the following error. I am using someone else's code and
all they are doing is pass an argv to a function then
def execute_action(manager, argv):
method_name =...
|
by: Bonavox |
last post by:
Hi!
FYI: I had some trouble with the type attribute in one of my providers
in web.config. I was writing it by hand, and got this error all the
time:
Required attribute 'type' not found....
|
by: Anthony England |
last post by:
I am considering general error handling routines and have written a sample
function to look up an ID in a table. The function returns True if it can
find the ID and create a recordset based on...
|
by: Anthony England |
last post by:
(sorry for the likely repost, but it is still not showing on my news server
and after that much typing, I don't want to lose it)
I am considering general error handling routines and have...
|
by: n4ixt |
last post by:
I have two macros I used to use in VS 2003. I recently tried to import them
for use in VS 2005, but they don't seem to work. I open the macro explorer,
right click and do run, but it's like VS...
|
by: Teja |
last post by:
Hi all,
What is attribute error? what causes that error, especially with COM
objects?
To be precise :
Attribute Error: LCAS.LabcarController.writeLogWindow()
Here, LCAS.... is a COM...
|
by: johnny |
last post by:
I am getting the following errors:
File "H:\xampp\xampp\xampp\python\lib\httplib.py", line 679, in
_send_output
self.send(msg)
File "H:\xampp\xampp\xampp\python\lib\httplib.py", line 646, in...
|
by: Jason |
last post by:
Hi folks--
Basically, I have a pressing need for a combination of 5.2 "Sorting a
List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects
by an Attribute of the Objects" from the...
|
by: MeeMee |
last post by:
Hi,
I have a problem in one of the MS Access systems at work. I am using some database functions that requires the DAO reference. The system is working fine on my PC, however it occurs on the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |