[Tutor] Re: Tutor Digest, Vol 14, Issue 32

2005-04-09 Thread Rick Muller
On Apr 9, 2005, at 1:54 PM, [EMAIL PROTECTED] wrote:
Message: 8
Date: Sat, 09 Apr 2005 20:58:49 +0200
From: Logesh Pillay [EMAIL PROTECTED]
Subject: [Tutor] quicksort using list comprehension
To: Discussion for learning programming with Python
tutor@python.org
Message-ID: [EMAIL PROTECTED]
Content-Type: text/plain; format=flowed; delsp=yes;
charset=iso-8859-15
I'm trying to program quicksort using list comprehension.
The following gives me a type mismatch error for +.
def qsort (t):
 if len (t)  1:
 return qsort([x for x in t[1:] if x = t[0]]) + [t[0]] + 
qsort([x
for x in t[1:] if x  t[0]])

I know this sounds strange but I have and idea it (or something 
similar)
worked when I last tried Python a yr or so ago. An earlier version of
Python?

Logesh Pillay
I think the following recipe is what you want:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66473
Rick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Recursive list checking

2005-04-08 Thread Rick Muller
On Apr 8, 2005, at 11:37 AM, [EMAIL PROTECTED] wrote:
From: joe_schmoe [EMAIL PROTECTED]
For example, this is what I am currently doing:
=code block 
# generate unique numbers and append to list
nmbr01 = random.randrange( 1, 20 )
nmbr_list.append( nmbr01 )
nmbr02 = random.randrange( 1, 20 )
# check for duplicates and re-generate a number if needed
while nmbr02 in nmbr_list:
nmbr02 = random.randrange( 1, 20 )
nmbr_list.append( nmbr02 )
nmbr03 = random.randrange( 1, 20 )
while nmbr03 in nmbr_list:
nmbr03 = random.randrange( 1, 20 )
nmbr.append( nmbr03 )

Since you want unique entries, couldn't you just do something like
def unique_entries(n,start=1,stop=20):
Generate n unique entries in range(1,20)
from random import shuffle
l = range(start,stop)
shuffle(l)
return l[:n]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Address book sort of

2004-12-04 Thread Rick Muller
Couldn't tell if this got sent, so I'm re-sending.
Apologies for duplicates:

Well, one option is to use pickle (or cPickle,
preferrably) to dump the python objects to a file:


from cPickle import load, dump

def save(fname,addressbook):
file = open(filename,'w')
dump(addressbook,file)
file.close()
return

def read(fname):
file = open(filename)
addressbook = load(file)
file.close()
return addressbook

The advantage of pickle is that you don't have to
decide on a text format for your data -- it just dumps
and then reloads the python code. You can waste a lot
of time deciding on a text format, implementing the
readers/writers, etc.

Rick

--- Eri Mendz
[EMAIL PROTECTED]@bluebottle.com wrote:

 Dear Tutor,
 
 I like to know what is the proper procedure (is
 algorithmn the right
 term?) in creating data in a program, write it to
 file, close the app
 then retrieve the data when run again. Basically,
 I'm trying to simulate
 a simple address book (well not really for the datas
 are just names for
 now) and so far have created the basic menu
 interface. It is console
 base so forget gui. I ask user input and store it in
 a list. There are
 menus to change, delete the data, and to save the
 data list in file. I
 use cPickle for this and have verified the file is
 created by checking
 in my $PWD. I want to retrieve that data when
 program is run again. What
 to add in my code? I thought not to post the code
 but explain it as
 above.
 
 What i want: when program is run again, the saved
 data is loaded when user
 selects option 1 below. Of course the first time it
 is run, the list is
 empty.
 
 def print_options():
print '''
Options:
[1] - Print content of list
[2] - Add name to list
[3] - Delete name from list
[4] - Change name in list
[5] - Save list to file
[P] - Print this menu
[Q] - Quit
'''
 
 
 
 -- 
 Regards,
 Eri Mendz
 Using PC-Pine 4.61
 
 
 -- 
 Using PC-Pine 4.61
 
 ___
 Tutor maillist  -  [EMAIL PROTECTED]
 http://mail.python.org/mailman/listinfo/tutor
 

=
Rick Muller
[EMAIL PROTECTED]



__ 
Do you Yahoo!? 
Yahoo! Mail - Find what you need with new enhanced search.
http://info.mail.yahoo.com/mail_250
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor