Re: [Tutor] List problem

2011-07-25 Thread Peter Otten
David Merrick wrote:

> def append(self,item):
>  '''Adds an item to the end of the List'''
> 
>  current = self.head
>  previous = None
>  while current.getNext() != None:
>  previous = current
>  current = current.getNext()
>  if current.getNext() == None:
>  previous  = previous.setNext(current)
>  current = current.setNext(item)

> myList.append(24)

Your append() method expects item to be a Node instance, so you have to wrap 
your data (24 in the example) into a Node

myList.append(Node(24))

or modify append() accordingly. Note that there is at least one other 
problem with your append() implementation: you cannot append to an empty 
UnorderedList because you don't handle the case where self.head is None.

Stylistically your code looks like a literal translation from Java; in 
Python it is good practice to avoid getter/setter methods and use attributes 
(or properties) instead. Also, we have a cool way to implement iteration: 
generators.

#untested
class UnorderedList(object):
def __iter__(self):
current = self.head
while current is not None:
yield current.data

You can then write

print 24 in myList

instead of

print myList.search(24)

In idiomatic Python you'd call the length() method __len__() and invoke it 
as

print len(myList)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List problem

2011-07-24 Thread Andre Engels
On Mon, Jul 25, 2011 at 4:19 AM, David Merrick  wrote:


> def append(self,item):
>  '''Adds an item to the end of the List'''
>
>  current = self.head
>  previous = None
>  while current.getNext() != None:
>  previous = current
>  current = current.getNext()
>  if current.getNext() == None:
>  previous  = previous.setNext(current)
>  current = current.setNext(item)
>

current is here the first element of your list. This element does not have a
getNext() method. Instead, if you want to use this system, you'll have to
use a (probably re-defined) getNext() method of your list class itself.

-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List problem

2011-07-24 Thread bob gailer
I have no desire to wade through all that code. Please post the entire 
traceback.



On 7/24/2011 10:19 PM, David Merrick wrote:

class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None

def getData(self):
return self.data

def getNext(self):
return self.next

def setdata(self,newData):
self.data = newData

def setNext(self,newnext):
   self.next = newnext

class UnorderedList:

def __init__(self):
self.head = None

def isEmpty(self):
return self.head == None

## Adds next item on to the head
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp

def length(self):
current = self.head
count = 0
while current !=None:
count = count + 1
current = current.getNext()
return count

def search(self,item):
current = self.head
found = False
while current != None and not found:
if current.getData()== item:
found =True
else:
current = current.getNext()
return found


def remove(self,item):
'''Removes item from the List'''

current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())

def getIndex(self,item):
current = self.head
index = 0
found = False
while current != None and not found:
if current.getData()== item:
found = True
else:
current = current.getNext()
index = index + 1
return index

def append(self,item):
 '''Adds an item to the end of the List'''

 current = self.head
 previous = None
 while current.getNext() != None:
 previous = current
 current = current.getNext()
 if current.getNext() == None:
 previous  = previous.setNext(current)
 current = current.setNext(item)


myList = UnorderedList()
myList.add(31)
myList.add(77)
myList.add(17)
myList.add(93)
myList.add(26)
myList.add(54)
print(myList.length())
myList.append(24)
print(myList.length())
myList.search(24)

Output

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
>>> [evaluate unorderedList.py]
6
builtins.AttributeError: 'int' object has no attribute 'getNext'
>>>

What do I need to do the append method to fix it?

--
Dave Merrick

merrick...@gmail.com 

Ph   03 3423 121
Cell 027 3089 169


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list problem

2007-02-21 Thread Kent Johnson
> Kirk Bailey wrote:
>> ok, here comes some code:
>>
>> f1=open(pagename,'r')
>> page=f1.readlines()
>> f1.close()
>>
>> at the end of which, the data is in page, which is a list. But 
>> something strange is going on here. all the data is in a single cell! 
>> it's a one cell list! Say what?

It sounds like for some reason the newer Python is not recognizing the 
line endings in the file. I'm not sure why that would be; are you 
running both versions on the same OS? That could cause different 
behaviour since the default line ending is different on Windows and 
Linux, for example.

Try opening the file with universal line endings:
f1 = open(pagename, 'Ur')

Kent

>>
>> Later on, when we try to itenerate the list and do things line by 
>> line, it takes the entire thing at one swallow, and this creates some 
>> trouble.
>>
>> Here's is a link to the entire program.
>> http://www.tinylist.org/MW.txt
>> this is the reader engine for a wiki to be used in a windows environment.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list problem

2007-02-21 Thread Michael Lange
On Wed, 21 Feb 2007 21:21:26 +1300
"John Fouhy" <[EMAIL PROTECTED]> wrote:

> > Kirk Bailey wrote:
> > > ok, here comes some code:
> > >
> > > f1=open(pagename,'r')
> > > page=f1.readlines()
> > > f1.close()
> > >
> > > at the end of which, the data is in page, which is a list. But
> > > something strange is going on here. all the data is in a single cell!
> > > it's a one cell list! Say what?
> 
> Have you tried looking at pagename in a text editor?  If readlines()
> is returning only one line, then you should be able to spot that in
> the file.
> 

Just a guess: if the behavior changed in between Python-2.3 and 2.5 ,
maybe it is an issue with line endings, LF vs. CR-LF ?

Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list problem

2007-02-21 Thread John Fouhy
> Kirk Bailey wrote:
> > ok, here comes some code:
> >
> > f1=open(pagename,'r')
> > page=f1.readlines()
> > f1.close()
> >
> > at the end of which, the data is in page, which is a list. But
> > something strange is going on here. all the data is in a single cell!
> > it's a one cell list! Say what?

Have you tried looking at pagename in a text editor?  If readlines()
is returning only one line, then you should be able to spot that in
the file.

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list problem

2007-02-20 Thread Luke Paireepinart
Kirk:
Please reply to this message, not the other one I sent,
and please reply on-list in the future, using the 'reply-all' button 
rather than 'reply.'
Otherwise the message just goes to me instead of to everyone, which is 
the default on this list.
This copy of your e-mail is forwarded to the list, so use a 'reply-all' 
on it so everyone can see your reply.
-Luke


Original e-mail:

Kirk Bailey wrote:
> ok, here comes some code:
>
> f1=open(pagename,'r')
> page=f1.readlines()
> f1.close()
>
> at the end of which, the data is in page, which is a list. But 
> something strange is going on here. all the data is in a single cell! 
> it's a one cell list! Say what?
>
> Later on, when we try to itenerate the list and do things line by 
> line, it takes the entire thing at one swallow, and this creates some 
> trouble.
>
> Here's is a link to the entire program.
> http://www.tinylist.org/MW.txt
> this is the reader engine for a wiki to be used in a windows environment.
>
>
>
> Luke Paireepinart wrote:
>> Kirk Bailey wrote:
>>> ok, getting back to python and wikiness, I have a problem, this 
>>> software of mine seems to exibit different behavior under the latest 
>>> edition of python (2.5) than under the version used when I first 
>>> wrote it (2.3).
>>>
>>> It loads the page file, but returns it as a list (which is correcft) 
>>> of one element, the entire file is in one cell. Prior, it returned 
>>> each line as an element in the list. this is causing me some 
>>> processing problems, and I am not a happy camper.
>>>
>>> I am still getting out the WD-40 and loosening up rusty hinges and 
>>> joints oin my python processing prefrontals, it's been quite a 
>>> while. I cna post the current program to a website if it would help, 
>>> or send it to you off list directly.
>>>
>>> The idea is to use MiniWiki in one's windoze laptop as a 
>>> wiki/notebook. Wikinehesa is optimied for freebsd/linux and works 
>>> fine as is.
>>>
>>> Discussion on or off list is saught. Constructive criticism will be 
>>> graciously received and thanked.
>>>
>>>   
>> If you could give us a snippet of input data,
>> as well as the function and the 10 or so lines preceding and 
>> following it, that returns a single-element list,
>> we could probably help.  Or better yet, write a new function that's 
>> very simple that just displays the behavior you don't want.
>> It sounds like you have a lot of code, though, and since you know 
>> where the problem is occurring it's easier if you give us an excerpt,
>> the cliffs notes version, if you will, than for one of us to read 
>> through your code.
>> -Luke
>>
>>
>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list problem

2007-02-20 Thread Rikard Bosnjakovic
On 2/21/07, Kirk Bailey <[EMAIL PROTECTED]> wrote:

[...]
> Discussion on or off list is saught. Constructive criticism will be
> graciously received and thanked.

Without links, pointers, code or anything grippable I find it
difficult to comment or discuss anything, since i haven't got the
faintest idea or your setup, environments, applications, and the like.

How about giving it another go, feeding us with more info?

-- 
- Rikard.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list problem

2007-02-20 Thread Luke Paireepinart
Kirk Bailey wrote:
> ok, getting back to python and wikiness, I have a problem, this software 
> of mine seems to exibit different behavior under the latest edition of 
> python (2.5) than under the version used when I first wrote it (2.3).
>
> It loads the page file, but returns it as a list (which is correcft) of 
> one element, the entire file is in one cell. Prior, it returned each 
> line as an element in the list. this is causing me some processing 
> problems, and I am not a happy camper.
>
> I am still getting out the WD-40 and loosening up rusty hinges and 
> joints oin my python processing prefrontals, it's been quite a while. I 
> cna post the current program to a website if it would help, or send it 
> to you off list directly.
>
> The idea is to use MiniWiki in one's windoze laptop as a wiki/notebook. 
> Wikinehesa is optimied for freebsd/linux and works fine as is.
>
> Discussion on or off list is saught. Constructive criticism will be 
> graciously received and thanked.
>
>   
If you could give us a snippet of input data,
as well as the function and the 10 or so lines preceding and following 
it, that returns a single-element list,
we could probably help.  Or better yet, write a new function that's very 
simple that just displays the behavior you don't want.
It sounds like you have a lot of code, though, and since you know where 
the problem is occurring it's easier if you give us an excerpt,
the cliffs notes version, if you will, than for one of us to read 
through your code.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor