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
-10 < n < 10
? \$\endgroup\$