23
1
A pyramidal matrix is a square matrix where all numbers increase or decrease from the center point, like the two matrices below:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
Or:
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
Given a non-zero integer n
, create a pyramidal matrix where the numbers goes from 1
to n
either in increasing order (if n<0), or decreasing order (if n>0) from the center. If n
is even, then there will be 4 center numbers (see the examples).
As always:
- Optional input and output format
- Number of spaces, delimiter etc. is optional
Test cases:
1
1
-1
1
5
1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 1
1 2 3 3 3 3 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 3 3 3 3 2 1
1 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1
-5
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
2
1 1 1 1
1 2 2 1
1 2 2 1
1 1 1 1
-2
2 2 2 2
2 1 1 2
2 1 1 2
2 2 2 2
-4
4 4 4 4 4 4 4 4
4 3 3 3 3 3 3 4
4 3 2 2 2 2 3 4
4 3 2 1 1 2 3 4
4 3 2 1 1 2 3 4
4 3 2 2 2 2 3 4
4 3 3 3 3 3 3 4
4 4 4 4 4 4 4 4
10Why is the even case different from the odd case? There is no reason why the matrices can't all follow the exact same pattern. – Greg Martin – 2016-09-22T16:32:23.387
2Because the input was supposed to be the length of the side wall, in which case there's a difference between odd and even. I decided to go for the maximum value instead, but kept the odd and even difference there. It might seem strange, and might not be a good explanation, but it's the explanation for why there's a difference. :-) – Stewie Griffin – 2016-09-22T17:20:09.433
2Can we assume
-10 < n < 10
? – Titus – 2016-09-22T18:43:45.8332It's ok if it doesn't look like a perfect square, as long as it it's one numerically speaking. If rows with many 10s are wider that those with few 10s then that's ok... – Stewie Griffin – 2016-09-22T19:52:57.860