# a question regarding conciseness

Christophe Delord christophe.delord at free.fr
Wed Feb 20 23:33:03 CET 2002

```Gerson Kurz wrote:

> On Wed, 20 Feb 2002 21:48:52 +0100, Christophe Delord
> <christophe.delord at free.fr> wrote:
>
>
>>Writting a function to sort lists may not be efficient :
>>
>>def my_sort(L):
>>    L2 = L[:]
>>    L2.sort()
>>    return L2
>>
>>X = [...]
>>for x in my_sort(X):
>>    ....
>>
>
> Of course, you can always use quicksort:
>
> q=lambda x,o=filter,r=lambda o:(lambda x=list(o)+[0],\
> y=len(o)-1:(lambda:map(lambda i,s=x.__setitem__:s(-1,\
> x[i])or s(i,x[y-i])or s(y-i,x[-1]),range((y+1)/2))and\
> 0)() or (lambda:"".join(x[:-1]))())(),s=')]1=<)x(nel'\
> '[]"x",))"o(lambda i,j=x[0]:i"(r,"~"(ecalper.)"q(~<j'\
> ',x))+~==j,x)+q(~>j,x))"(r[(lave':eval(r(s))
>
> import random
> test = range(20)
> random.shuffle(test)
> print test, "->", q(test)
>
>

Not obvious at the first look (lots of eval and reverse) but very funny.
I ran it by hand and wrote an easier to read qsort :

q=lambda x:(lambda o=lambda s:filter(lambda i:cmp(i,x[0])==s,x):len(x)>1
and q(o(-1))+o(0)+q(o(1)) or x)()

It uses nested scopes so it needs python 2.2.
For previous versions it may be :

q=lambda x:(lambda o=lambda s,x=x:filter(lambda
i,x=x:cmp(i,x[0])==s,x):len(x)>1 and q(o(-1))+o(0)+q(o(1)) or x)()

--
Christophe Delord
http://christophe.delord.free.fr

```