The words "center a" probably give flashbacks to any decent HTML developer. Luckily, we are not working with divs, we are working with matrices.
Given a matrix where w=h, and given a "length" to extend it by, make a new matrix and center the old one in it, populating blank squares with the length. For example:
m = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
l = 1
OUT = [
[1, 1, 1, 1, 1],
[1, 1, 2, 3, 1],
[1, 4, 5, 6, 1],
[1, 7, 8, 9, 1],
[1, 1, 1, 1, 1]]
m = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
l = 2
OUT = [
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 2, 3, 1, 1],
[1, 1, 4, 5, 6, 1, 1],
[1, 1, 7, 8, 9, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1]
I would do more examples but they are tough to type out :/
Notice that each dimension has length*2 added to it. And that the "empty" space (the new matrix) is populated with the length. Here are the parameters and rules:
Params
- vars / method can be named whatever you want
- 0 < m.length < 11
- -1 < m[x][y] < 11
- 0 < l < 11
Rules
- Input will be
f(int[][] m, int l)
- Output will be
int[][]
- Use the closest thing to a matrix if not available in your language
- Standard rules apply
- This is
code-golf
, so shortest code per language wins!
Good luck :)