2005-12-29 20:04

Ok, here is how you write the shortest one

About http://www.pycontest.net... here's how it's done.

Sadly, it's pretty much impossible to put the code on the web because it has some really non-ascii stuff in it ;-)

Here's the key tricks (thanks to Remi and the others):

One of the key tricks is writing a short version of a very long octal number (or three).

I tried using base36 and int('gobledygook',36).

You could use 0xhexacodehere.

But here's the most space-efficient one: use base 256!

For each byte you want, create a char using chr(byte). Then paste them all together. Then put it in your code as a string. If you are lucky you will have no quotes or newlines in it, and the python interpreter will eat it.

You can later get the byte number x using ord('h!alsdf'[x]), and each byte by some dividing and modulo operation.

Another important piece is encoding 7 3-character strings as compactly as possible, and splitting them by offsets (which are encoded in the previosuly mentioned big number in base 8 ;-)

One extra squeeze is finding the shortest way to concatenate strings.

It's ''.join(), but if you are using it twice (or more), you save space by doing j=''.join and using j later.

Last one: Instead of defining a function, do a lambda:

def seven_seg(x): return .....

seven_seg=lambda x:

6 chars less!

And here is the shortest code I got so far (121 characters, requires python 2.4. On 2.2, it has to be 124).

In the contest, some guys are at 120. I don't know how. I can't even guess how ;-)

Update: it turns out I did know how to do it check it out I think it's the first public 120 :-)

BTW: you can't write that using kwrite. Vim works, though.


Comments powered by Disqus

Contents © 2000-2018 Roberto Alsina