Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Python Contest

There is a python con­test at­con­

The task is writ­ing the short­est pro­gram to drive a sev­en-seg­ment LCD thingy.

I have no hope of win­ning, but here's a help­ful hin­t:

If your code is any longer than this (191 chars), it will not win ;-)

a=' _ '
c='   '
d='  |'
e=' _|'
f='|_ '
g='| |'
def seven_seg(x):
        return '\n'.join([eval('+'.join([v[int(l)*3+i]for l in x]))for i in 0,1,2])+'\n'

Note: I edit­ed this item way too many times al­ready ;-)

And yes, I can save two char­ac­ters mov­ing the re­turn up.

A much uglier, yet much short­er (151) ver­sion:

def seven_seg(x):return''.join([''.join(['|    ||__  __ || |  |'[int('a302ho6nqyp9vxvpeow',36)/10**(int(l)*3+u)%10::7]for l in x])+'\n'for u in 0,1,2])

And yes, that pret­ty much proves you can write ug­ly python. And I am giv­ing up. A short­er ver­sion prob­a­bly in­volves a dif­fer­ent al­go­rith­m, and I can't find any.

I am par­tic­u­lar­ly proud of sav­ing one char­ac­ter by writ­ing 104004334054154302114514332064 as in­t('a302ho6n­qyp9vxvpe­ow',36).

Al­so in­ter­est­ing is that the num­ber I was us­ing there orig­i­nal­ly start­ed with 4 and was ex­act­ly the same length writ­ten both ways ;-)

Since that num­ber is pret­ty ar­bi­trary (it's an in­dex ta­ble in­to the "graph­ic­s" ar­ray), I just shuf­fled the 1 and the 4. The 0 would have been bet­ter but then it did­n't work, of course :-)

ZeD / 2006-04-04 12:39:

Now I'm at 137, but on comp.lang.python newsgroup I've seen a 125 char solution :P

Roberto Alsina / 2006-04-04 12:41:

How about sharing the code Remi? ;-)

Remi Villatel / 2006-04-04 12:41:

I managed to reduce your code to 135 bytes... but that gave me nothing better than rank 25. I'm still miles away from the winning 120 bytes.


Remi Villatel / 2006-04-04 12:42:

Any way, I won't win... So, why not?


def seven_seg(z):return''.join(''.join(

' | ||_ ___ ||| |'


for a in z)+"n"for u in(64,8,1))


This is a one-liner. I just cut it in a better way than the automatic formating. Don't add spaces anywhere and don't press RETURN at the end of the line. Voilà: 135 bytes of pure ugliness. ;-)

Remi Villatel / 2006-04-04 12:42:

I just cut and pasted my code from Konqueror to Kate. So, if you get 136 bytes instead of 135, remove the space before "for a in z". It is unnecessary, the same way than before "for u in ()".

Remi Villatel / 2006-04-04 12:43:

Here is also the 142-byte one-liner I was so proud of because of the 3 nested generators. Not very far by the look but very different though.


def seven_seg(z):return''.join(''.join(''.join(

" _| |"[ord("wx12][:koRx7f{"[int(a)])/b&7&n;]

for n in(4,1,2))for a in z)

+"n"for b in(64,8,1))


The same advices apply: Don't add spaces, no RETURN.

phone number lookup / 2011-12-03 22:42:

this is really interesting viewpoint on the subject i might add

employment background check / 2011-12-27 23:32:

Well, the write-up is truly the freshest on this laudable topic. 

Contents © 2000-2024 Roberto Alsina