I told you I was an addict. Problem statement is here.

This simple challenge requires you to calculate the maximum value found by summing all the rows and columns of a grid of numbers.

And you want to do it in the minimum number of characters (e.g. golf strokes) in your programming language.

They give an example matrix, and their result (which is correct).

So … what can you do for this?

I used two languages: Octave/Matlab and Perl. The former is more of a ‘modeling’ language with formal programming bits atop it, and the latter is a classical programming language, quite notorious for its ability to be terse.

Here’s the simplest Octave/Matlab I could come up with to do this:

octave:11> g=gc1 g = 1 34 46 31 55 21 16 88 87 87 32 40 82 40 43 96 8 82 41 86 30 16 24 18 4 54 65 96 38 48 32 0 99 90 24 75 89 41 4 1 11 80 31 83 8 93 37 96 27 64 9 81 28 41 48 23 68 55 86 72 64 61 14 55 33 39 40 18 57 59 49 34 50 81 85 12 22 54 80 76 18 45 50 26 81 95 25 14 46 75 22 52 37 50 37 40 16 71 52 17 octave:12> max(norm(g,'inf'),norm(transpose(g),'inf')) ans = 615

That one line is 43 characters. Took me about 30 seconds to do that.

I wish the Perl were that good, but I couldn’t figure out how to do a particular print the last element of list I sort in a single statement. Must be having a brain block.

Will post this code when I get home.

Viewed 13120 times by 2872 viewers

I assume APL would be considered cheating…

@Jason

Not at all …

It would look something like

+/ …

and some other things (wow … forgot all I knew about APL). Post it if you can.

From memory – no prompt to hand – this should do the trick in an ascii variant of apl..

|/+/’g,+:g

(“max over the sums of each row and column of g”)

@Niall

Yeah … seems to be about right ðŸ™‚ There is much to be said for terse languages …

I cut my eye teeth on APL in the ’70s. But surprisingly, in the ’90s, couldn’t get into J.

@Peter Shenkin

J would be different again – I used k originally, where a j solution is something like this:

>./(+/”1,+/) g

where

>./ is “greater over”

+/”1 is “row sums”

+/ is “col sums”

applied to g

J is a really neat and powerful language – a little tough for those with conservative neural pathways from too much Java etc. (scarily those worst afflicted by such set-in-stone pathways are some of my younger colleagues).

There actually is a usable APL environment available under Emacs+aplus, too. The http://www.emacswiki.org/emacs/AplInDebian page talks about Debian, but the pieces are quite well ported.

I had it working at one point, but uncontrolled version drift knocked it over. I don’t have many opportunities to play with APL or J at the moment, although the temporal database side of K is really interesting and relevant.

Here is the perl version:

map{map{$r[$i/10]+=$_;$r[$i%10+10]+=$_;$i++}/(\d+)/g};@x=sort@r;print pop @x;

The problem with this is that I can’t easily do a vector update … I’d really like to do

$r[$i/10,$i%10+10]+=($_,$_);

Sort of a scatter operation. Perl doesn’t really have intrinsics to support row or column scans. The regex engine is very powerful and terse, but I haven’t really figured out how to do this.