Function to print a diamond shape

bearophile bearophile at HUGS
Mon Mar 24 08:07:46 PDT 2014


On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote:
> This is a somewhat common little exercise:

if you like similar puzzles, here is another:

Write a program that expects a 10-by-10 matrix from standard 
input. The program should compute sum of each row and each column 
and print the highest of these numbers to standard output.

     An example input:

     01 34 46 31 55 21 16 88 87 87
     32 40 82 40 43 96 08 82 41 86
     30 16 24 18 04 54 65 96 38 48
     32 00 99 90 24 75 89 41 04 01
     11 80 31 83 08 93 37 96 27 64
     09 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

     Expected output:

     615

The purpose is to write a "golfing" program, that is the shortest.

My current D solution is about 170 bytes (UNIX newlines):

void main(){
import std.stdio,std.range,std.algorithm,std.conv;
auto m=10.iota.map!(_=>readln.split.to!(int[]));
m.map!sum.chain(m.transposed.map!sum).reduce!max.write;
}


I am now trying to use std.file.slurp, but its documentation is 
insufficient.

A cryptic Python solution (not mine), 73 characters:

m=[map(int,_().split())for _ in[raw_input]*10]
_(max(map(sum,m+zip(*m))))

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list