My name is Paul Bissex, and e-scribe.com is my consulting business. I build web applications using as much open source software as possible. From September to June I teach web design and other important non-photographic professional skills to photographers. In the '90s I wrote technology commentary and reviews for magazines, newspapers, and web publications, including Wired, Salon.com, FamilyPC, the late lamented Web Review, and the Chicago Tribune. Feel free to email me.
I'm co-authoring a book, "Python Web Development with Django", with Jeff Forcier and Wesley Chun. It will be published by Prentice Hall in July 2008, but is available for pre-ordering on Amazon now.
This site is built on a fresh trunk checkout of Django, running on Python 2.5.1, served by Apache and mod_python. The database is SQLite. The operating system is FreeBSD, on a VPS hosted at Johncompanies.com. Comment-spam protection by Akismet. Vintage topo imagery from the Maptech archive.
Akismet, del.icio.us, Django, dpaste.com, Emacs, FreeBSD, Freenode, jQuery, LaunchBar, MacPorts, Markdown, Mercurial, OS X, Postfix, Python, SQLite, Subversion, TextMate, Trac, Ubuntu Linux, wmii
Copyright 2008
by Paul Bissex
and E-Scribe New Media
lambdas in a single line and weighs in at 105 bytes. I thought this might be the best possible in Python 2.4 and earlier, but in fact a simpler version can be constructed using the old short-circuit logic trick, and at 94 bytes it's even smaller than my original. Here it is: q=lambda s:len(s)and q([x for x in s[1:]if x<s[0]])+[s[0]]+q([x for x in s[1:]if x>=s[0]])or sEnticed by the lovely Haskell quicksort example, and sullied by the code-crunching ways of Codegolf, I decided to see how small a Python quicksort function I could write. I stopped at 99 bytes.
>>> q=lambda s:s if len(s)<2 else q([x for x in s[1:]if x<s[0]])+[s[0]]+q([x for x in s[1:]if x>=s[0]])
>>> print q([9,7,5,3,1,8,6,4,2])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
(If that's hard to read, try the colorized version.)
This requires Python 2.5 because of its use of the new ternary x-if-y-else-z syntax. Please, no style comments! Unreasonable compression is the whole point. I have to say, though, that in terms of readability Python holds up better than most languages under this kind of treatment. Not so much in this example, though...
Here is a slightly shorter program at 86 bytes:
q=lambda s:s and q([x for x in s[1:]if x=s[0]])
And even shorter, at 82 bytes:
e='q([x for x in s[1:]if x';q=lambda s:s and eval(e+'=s[0]])'%e)
Let's try that again:
q=lambda s:s and q([x for x in s[1:]if x<s[0]])+[s[0]]+q([x for x in s[1:]if x>=s[0]])
e='q([x for x in s[1:]if x';q=lambda s:s and eval(e+'<s[0]])+[s[0]]+%s>=s[0]])'%e)
Slightly rearranging gets a 79 byte version:
e='q([x for x in s[1:]if s[0]';q=lambda s:s and eval(e+'>x])+[s[0]]+%s<=x])'%e)
Is this really quicksort in terms of how many opertations this consumes? It seems to me that this version goes through each intermediate list twice, where as the canonical version of quicksort should do it only once. Of course the canonical version also does two operations inside the iteration.
I'd have to go through this with paper and pen, but will Santa approve me writing down pseudocode at the Xmas dinner?
It'd be enlightening to do performance comparisons between this code and the "old way" of doing quicksort, though.
I believe the biggest factor that makes this not-a-real-quicksort is that it doesn't sort the items in place. I blame the Haskell guys for starting it!
A version which more closely resembles the original:
q=lambda s:[] if s==[] else q([x for x in s[1:]if x=s[0]])
This pretty closely resembles the canonical Haskell quicksort:
sort [] = []
sort (x:xs) = sort (filter (<x) xs) ++ [x] ++ sort (filter (>=x) xs)
And this reduces to a paltry 45 bytes:
q[]=[];q(h:t)=q[x|x<-t,x<h]++h:q[x|x<-t,h<=x]
An excellent example of why I think it would be interesting to add Haskell to Codegolf!
(I patched up your comment, BTW. Anti-spam measures make posting code a bit harder than it should be.)
Comments use Markdown syntax. Your comment will not appear until approved, which may take a few hours or more. Spammers will be torpedoed.
The iPhone keyboard doesn't suck
Python one-liner of the day
7 comments
How not to advocate via Google Code
2 comments
99 problems
3 comments
bitmonk
Obscure "svn mv" problem solved
54 days ago
Charlie
Book news: Rough Cuts and Amazon
55 days ago
Simon Griffee
Django Mercurial mirror tweaks
72 days ago
Jason Calleiro
From PHP to Python
73 days ago
Yuli
dpaste.com
76 days ago
bruce
Neat Python hack: infix operators
80 days ago
David Reynolds
The original Lego Star Wars
88 days ago
At least 31789 pieces of comment spam killed since January 12th. Thanks are mostly due to Akismet.
Isn't "def q(s):" shorter than "q=lambda a:" ?