An interesting Python programming contest ended today. The task was to write the shortest Python module for converting a string of digits to seven-segment display format. The module was to be used like this:

>>> from seven_seg import seven_seg
>>> print seven_seg('0123456789')
 _     _  _     _  _  _  _  _
| |  | _| _||_||_ |_   ||_||_|
|_|  ||_  _|  | _||_|  ||_| _|

Solutions were measured with the Unix command wc -c seven_seg.py.

The shortest solution (by André Roberge, who is apparenly some kind of a genius) is 117 characters long. My best try was this 120-character-long one-liner of obfuscated Python:

seven_seg=lambda i,j=''.join:j(j(' _   _|_|_  | |'[ord('f\xda($\xbaDFZ64'[int(d)])>>r&14:][:3]for d in i)+'\n'for r in[6,3,0])

(Replace \xda and \xba with actual 8-bit characters, and make sure the file has no trailing newline to get a 120-byte long file.)

There is also a 30-character-long cheat that fools the test suite into thinking it is a valid solution:

class seven_seg(str):__eq__=id

It was fun to read about various solutions by other people in this thread on comp.lang.python, and in other places.

Update: Read André's Deconstruction of the 117-character solution (with insightful readers' comments about the Chinese Remainder Theorem).