11
1
Given a list of numbers [ a1 a2 ... an ], compute the sum of all the matrices Aᵢ where Aᵢ is defined as follows (m is the maximum of all aᵢ):
1 2 ⋯ (i-1) i (i+1) ⋯ n
+----------------------------
1 | 0 0 ⋯ 0 aᵢ aᵢ ⋯ aᵢ
2 | 0 0 ⋯ 0 aᵢ aᵢ ⋯ aᵢ
. . . . . . . .
. . . . . . . .
aᵢ | 0 0 ⋯ 0 aᵢ aᵢ ⋯ aᵢ
aᵢ₊₁ | 0 0 ⋯ 0 0 0 ⋯ 0
. . . . . . . .
. . . . . . . .
m | 0 0 ⋯ 0 0 0 ⋯ 0
Example
Given the input [2,1,3,1]
we construct the following matrix:
[2 2 2 2] [0 1 1 1] [0 0 3 3] [0 0 0 1] [2 3 6 7]
[2 2 2 2] + [0 0 0 0] + [0 0 3 3] + [0 0 0 0] = [2 2 5 5]
[0 0 0 0] [0 0 0 0] [0 0 3 3] [0 0 0 0] [0 0 3 3]
Rules and I/O
- you may assume the input is non-empty
- you may assume all the inputs are non-negative (0≤)
- the input can be a 1×n (or n×1) matrix, list, array etc.
- similarly the output can be a matrix, list of lists, array etc.
- you can take and return inputs via any default I/O format
- your submission may be a full program or function
Test cases
[0] -> [] or [[]]
[1] -> [[1]]
[3] -> [[3],[3],[3]]
[2,2] -> [[2,4],[2,4]]
[3,0,0] -> [[3,3,3],[3,3,3],[3,3,3]]
[1,2,3,4,5] -> [[1,3,6,10,15],[0,2,5,9,14],[0,0,3,7,12],[0,0,0,4,9],[0,0,0,0,5]]
[10,1,0,3,7,8] -> [[10,11,11,14,21,29],[10,10,10,13,20,28],[10,10,10,13,20,28],[10,10,10,10,17,25],[10,10,10,10,17,25],[10,10,10,10,17,25],[10,10,10,10,17,25],[10,10,10,10,10,18],[10,10,10,10,10,10],[10,10,10,10,10,10]]
I'm guessing there's a font difference or something. I see you rolled back my edit. This is how it currently looks to me https://imgur.com/a06RH9r This is Chrome on Windows 10. The vertical ellipses are not rendered in monospace for some reason, and don't align with the columns. That's why I changed it. But I guess it must look different in different environments.
– recursive – 2018-05-23T16:37:37.0571Definitely a font issue. Both revisions are misaligned on my screen. – Dennis – 2018-05-23T16:41:53.330
May we return the result transposed? – Adám – 2018-05-23T20:08:40.527
1we need mathjax! – qwr – 2018-05-23T20:26:02.700
1@Adám: I'm gonna say no to that, however feel free to include a solution in your post that does so. – ბიმო – 2018-05-23T21:19:24.403