21 Apr 2004 fxn   » (Master)

It had been a while since I played Perl Golf. Today someone posted a mini-golf problem in the fwp mailing list, and since I woke up very early in the morning (4am) I decided to have some fun with it.

Given two strings $x and $y we have to put in $n the index of the first character where they differ. If the strings are equal $n has to be undef. For example, if $x is "foo" and $y is "foobar" then $n has to be 3.

I sent a few solutions and by now the shortest one is mine (with a fix pointed out by Yitzchak Scott-Thoennes):

    {$x ne$y&&$x=~/./sg&$y=~/\G\Q$&/g&&redo;$n=$-[0]}

It's a pity playing golf requires so much effort to squeeze a solution until you get something more or less good. It remembers me to how work with koans is explained in books (I have never worked with koans myself), you visualize your solution and try to improve, change, explore other approaches, consult the documentation to take advantage of corner cases, try to gain some character via some side-effect, reordering, think, think, think, ..., you have to be one with the problem, so to speak. Takes time, concentration, and it's very intensive. Solutions often come by saturation. Otherwise I would play more frequently.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!