31
0
Take a non-empty matrix / numeric array containing positive integers as input. Return, in this order, the sums of the first row and column, then the second row and column and continue until there aren't any more rows or columns.
Suppose the input is:
2 10 10 2 4
9 7 7 2 9
1 7 6 2 4
7 1 4 8 9
Then the output should be:
45, 33, 16, 17
Because: 2+9+1+7+10+10+2+4=45, 7+7+1+7+2+9=33, 6+4+2+4=16, 8+9=17
.
Test cases:
Test cases are on the following format:
Input
---
Output
5
---
5
..........
1 4
----
5
..........
7
2
---
9
..........
8 3 7 10 3 7 10 1
10 7 5 8 4 3 3 1
1 6 4 1 3 6 10 1
2 3 8 2 8 3 4 1
---
62 40 33 18
..........
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
---
320 226 235 263 135 26 20
..........
7 10 1
4 4 2
6 3 4
1 4 10
5 7 6
---
34 20 20
As arrays:
[[5]]
[[1,4]]
[[7],[2]]
[[8,3,7,10,3,7,10,1],[10,7,5,8,4,3,3,1],[1,6,4,1,3,6,10,1],[2,3,8,2,8,3,4,1]]
[[30,39,48,1,10,19,28],[38,47,7,9,18,27,29],[46,6,8,17,26,35,37],[5,14,16,25,34,36,45],[13,15,24,33,42,44,4],[21,23,32,41,43,3,12],[22,31,40,49,2,11,20]]
[[7,10,1],[4,4,2],[6,3,4],[1,4,10],[5,7,6]]
This is code-golf so the shortest solution in each language wins.
Would it be acceptable to print the sums and then print zeros forever? – Jonathan Allan – 2017-05-18T20:49:43.177
2@JonathanAllan, printing zeros forever is a bit of a stretch, so I think I must say no to that one. – Stewie Griffin – 2017-05-18T21:01:43.213
1Retina program to convert from pretty examples to Python arrays. – mbomb007 – 2017-05-18T21:43:15.120
1Looking at the examples. the task desciption is wrong. The second column in first example is
10,7,7,1
, the second row is9,7,7,2,9
and the sum is59
. And so on – edc65 – 2017-05-19T13:22:31.4971@edc65 Looking at the examples, it appears that numbers used in previous calculations aren't reused. Or another way, when considering nth row, only use values from the nth column on, and ignore those in columns 1 through n-1. – Brian J – 2017-05-19T13:28:17.783
@edc65 And rereading your comment, I see you are just asking the description be updated, not the examples, so nevermind :) – Brian J – 2017-05-19T13:29:06.617
Does the input have to be taken via STDIN or can it be a variable passed to a function? – Arc676 – 2017-05-25T11:58:41.753
1@Arc676 Standard io rules. Function arguments are one of the accepted input methods. – Stewie Griffin – 2017-05-25T12:05:52.433