Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about programming (old posts, page 10)

My first interesting hack

In the pre­vi­ous ar­ti­cle, some­one sug­gest­ed cheat­ing on ex­ams as an in­ter­est­ing ap­pli­ca­tion of telepa­thy.

Which re­mind­ed me of my first in­ter­est­ing hack.

I was in col­lege, back when com­put­er time was al­lo­cat­ed by the hour, and I had no com­put­er, and I was about to take my fi­nal ex­am on lin­ear pro­gram­ming.

For those who are not fa­mil­iar with the term, lin­ear pro­gram­ming is not re­al­ly about pro­gram­ming. It's about solv­ing a spe­cif­ic ind of op­ti­miza­tion prob­lem­s.

And for those who don't know that, don't swaet it, it re­duced, in re­al 1989 life, to ap­ply­ing a pro­gram called LIN­DO to find a lo­cal or glob­al min or max for a func­tion.

Now, we were a poor col­lege, so there were like 10 com­put­er­s. For 5000 stu­dents. And we, in that sub­jec­t, were not al­lowed to use it.

And it did­n't have lin­ear pro­gram­ming soft­ware in it any­way. And it had no com­pil­ers or in­ter­preters (ex­cept qba­sic).

So, we did LIN­DO by hand. On pa­per (the Sim­plex method­).

And it was bor­ing. But we did it. It is pret­ty sim­ple once you get the hang of it. But you have to make hun­dreds of cal­cu­la­tion­s, so we were al­lowed cal­cu­la­tors.

And I had this ba­by.

I had bought it with my sec­ond pay­check as a teach­er as­sis­tan­t. It cost­ed me about the same as a small bike.

Or a TV.

And it had 4KB of ram, an ascii key­board, and 116 pre­loaded pro­grams for com­mon sci­en­tif­ic cal­cu­la­tion.

It was the best cal­cu­la­tor I ev­er had :-)

And it was pro­gram­mable in BA­SIC.

So, the night be­fore the ex­am, as I did a sam­ple drill, I de­cid­ed to im­ple­ment a solver.

But since we had to solve things on pa­per, I had to show in­ter­me­di­ate step­s.

So, in­stead of a pro­gram to solve the prob­lem, I wrote a pro­gram that solved step-by-step as done by hand. Which was about 20x hard­er.

And it did fit and run in the 4KB of ram, and it dis­played the in­ter­me­di­ate re­sults on the 2x32 char screen.

Sad­ly, there was no way to take a pro­gram out of it, so it was lost on the next bat­tery change.

But hey, I think that was nice :-)

My first time

I just found here the an­nounce­ment of the first free soft­ware I pub­lished (at least, that I re­cal­l), from may 13, 1996. So, It's go­ing to be 10 years in 5 month­s!

Killer quote:

Python 1.3 (Maybe 1.2 would work too)
XForms 0.80 (NOT 0.75)

Ok, here is how you write the shortest one

About­con­ here's how it's done.

Sad­ly, it's pret­ty much im­pos­si­ble to put the code on the web be­cause it has some re­al­ly non-ascii stuff in it ;-)

Here's the key tricks (thanks to Re­mi and the oth­er­s):

One of the key tricks is writ­ing a short ver­sion of a very long oc­tal num­ber (or three­).

I tried us­ing base36 and in­t('­gob­ledy­gook',36).

You could use 0x­hex­a­code­here.

But here's the most space-­ef­fi­cient one: use base 256!

For each byte you wan­t, cre­ate a char us­ing chr(byte). Then paste them all to­geth­er. Then put it in your code as a string. If you are lucky you will have no quotes or new­lines in it, and the python in­ter­preter will eat it.

You can lat­er get the byte num­ber x us­ing or­d('h!als­d­f'[x]), and each byte by some di­vid­ing and mod­u­lo op­er­a­tion.

An­oth­er im­por­tant piece is en­cod­ing 7 3-char­ac­ter strings as com­pact­ly as pos­si­ble, and split­ting them by off­sets (which are en­cod­ed in the pre­vio­su­ly men­tioned big num­ber in base 8 ;-)

One ex­tra squeeze is find­ing the short­est way to con­cate­nate strings.

It's ''.join(), but if you are us­ing it twice (or more), you save space by do­ing j=''.join and us­ing j lat­er.

Last one: In­stead of defin­ing a func­tion, do a lamb­da:

def sev­en_seg(x): re­turn .....

sev­en_seg=lamb­da x:

6 chars less!

And here is the short­est code I got so far (121 char­ac­ter­s, re­quires python 2.4. On 2.2, it has to be 124).

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

Up­date: it turns out I did know how to do it check it out I think it's the first pub­lic 120 :-)

BTW: you can't write that us­ing kwrite. Vim work­s, though.

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 :-)

Forgotten Language: Jorf could have been Python (or Ruby)

A long time ago, there was no In­ter­net.

Ok, there was an in­ter­net, but I lived out­side of it. It was 1992 or 1993, and I on­ly saw my first web­page and send my first email in 1995. And I was per­haps the third per­son to have an ac­count on a per­ma­nent­ly in­ter­net-­con­nect­ed box in a 150 km ra­dius.

But that did­n't mean I had no ac­cess to in­ter­net stuff! What I did was buy CDs con­tain­ing mir­rors of repos­i­to­ries like Sim­ (it was a sin­gle CD, too!) and in there you could find hun­dreds of pro­gram­s.

Most of them share­ware, most of them crap, but ev­ery once in a while, there was some­thing cool, like DJGPP (a whole gcc suite for DOS! A re­al C com­pil­er! For Free!)

At the time, I had a side job writ­ing da­ta en­try soft­ware for sta­tis­tics stu­dents. They were sim­ple pro­grams that showed a for­m, where da­ta was load­ed, then it did some sim­ple ma­nip­u­la­tions of the da­ta.

The nat­u­ral lan­guage for that was some­thing like Clip­per or DBase, but I did­n't have ac­cess to them (or a way to learn it. Re­mem­ber, no In­ter­net).

On one of those Sim­tel CDs I found Jor­f. (Josephine's Recipe Fil­er). It was a OO lan­guage, with an in­ter­preter for DOS or Win­dows, and it sup­port­ed stuff that was re­al­ly ad­vanced for the time, and it made my cod­ing a lot sim­pler.

Out of nos­tal­gy, I down­load­ed a copy (yes, it is still there), and ran it in Dos­BOX (yes, it still work­s), to check if it was as good as I re­mem­bered.

You know what? It is.

In fac­t, if it had come out 2 or three years lat­er, and as free soft­ware in­stead of share­ware... I think it would have been big.

Here are some high­lights og the lan­guage:

  • OOP

  • Has in­­te­­grat­ed win­­dow­ing tool­k­it (for DOS and Win­­dows)

  • It had an in­­ter­ac­­tive hy­per­­tex­t/win­­dow­ing tu­­to­ri­al writ­ten in it­­self. In 1993.

  • It looks like a cousin of Python. A freaky cous­ing, though.

    1. Com­­­ments start with |

    2. Strings lim­it­ed with sin­­­gle or dou­ble quotes

    3. Au­­­to­­­mat­ic type con­ver­­­sions

    4. In­­­ten­­­ta­­­tion con­trols flow :-)

    5. No de­­­clared da­­­ta types

    6. In­­­te­­­grat­ed ed­i­­­tor and de­bug­ger

Sam­ple Hel­lo World:

  Msg:Add ("Quick Demonstration","Ok")
    Sure you can say "Hello World" in one line of
    C code. But how many punctuation characters
    are required to display a dialog box like this?
  Return (Ok)

That piece of code showed a win­dow with the mes­sage in it, and a Ok but­ton.

The funky thing is: in the tu­to­ri­al, you saw the in­te­grat­ed ed­i­tor open, and the text of the ex­am­ple start to ap­pear, and then it ran.

That looked like mag­ic at the time :-)

The tool­kit sup­port­ed ra­dio but­ton­s, check­box­es, text en­tries, all the ba­sic­s, and it was a thou­sand times eas­i­er than what Tur­bo Pas­cal or Tur­bo C guys bat­tled with at the time.

The au­thor was Way­land Brun­s. He lived, in 1993, in Colton, Ore­gon.

He lat­er seems to have be­come CTO of a com­pa­ny that de­signs sync soft­ware for Gold­mine, Lo­tus and oth­er such things.

So, he be­came a suit ;-). How­ev­er, he was once a guy that wrote, in his soft­ware's man­u­al, things like:

JORF Com­pa­ny is just me, Way­land Brun­s. I have been work­ing on JORF for six years, and ran out of mon­ey three years ago.


JOR­F(R) is a new com­put­er lan­guage. JORF was cre­at­ed by a Grun­t-pro­gram­mer frus­trat­ed by low lev­el math based com­put­er lan­guages that are in­ap­propi­ate for busi­ness da­ta pro­cess­ing.

And you know what? It was the right idea. If he start­ed Jorf in 1987, that means he start­ed it around the same time Perl 1.0, (and the syn­tax is much nicer ;-). He start­ed it around the same time Gui­do start­ed Python.

Here's a toast to JOR­F, which could have been Per­l, or Python, or Ru­by. But was not.