[Tutor] quicksort using list comprehension

2005-04-09 Thread Logesh Pillay
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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] quicksort using list comprehension

2005-04-09 Thread Kent Johnson
I think you have to return a value when len(t) = 1. You don't return anything which means you 
return None which can't be added to a list.

Kent
Logesh Pillay wrote:
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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] quicksort using list comprehension

2005-04-09 Thread Max Noel
On Apr 9, 2005, at 21:50, Kent Johnson wrote:
I think you have to return a value when len(t) = 1. You don't return 
anything which means you return None which can't be added to a list.

Kent
	Yup. Here's a quicksort I did, adapting an example from the Wikipedia 
article on Haskell:

def qsort(toSort):
if toSort == []:
return []
else:
x = toSort[0]
elts_lt_x = [y for y in toSort if y  x]
elts_gt_x = [y for y in toSort if y  x]
return qsort(elts_lt_x) + [x] + qsort(elts_gt_x)

	It is, of course, nowhere near as optimal as a real quicksort 
algorithm (like the one the list.sort method implements).

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?

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