I like to pretty print multidimensional arrays, like this:
[ [ [1, 2, 3],
[4, 5, 6] ],
[ [7, 8, 9],
[6, 4, 2] ] ]
But it's a pain to do by hand and it'd be nice to have a program that does this for me. Your challenge is to create a program that does this for me, taking a multidimensional array containing only positive integers and prettyprinting it.
Specifically, an array of depth 1 is printed joined by ,
with [
prepended and ]
appended:
[1, 2, 3]
An array of depth \$n+1\$, which contains at least one array of depth \$n\$, has its subarrays prettyprinted, joined by newlines and indented by two spaces. All but the last subarray have a comma appended, the last has ]
appended, and the first has its first line indented with [
instead of two spaces:
Here's a reference implementation:
function recursivePrettyPrint(array){
if(array.every(x => typeof x == "number")){
return `[${array.join(', ')}]`;
} else {
return array.map((item, index) => {
let result = recursivePrettyPrint(item) + ',';
result = result.split`\n`;
if(index == 0){
result[0] = '[ ' + result[0];
} else {
result[0] = ' ' + result[0];
}
for(let i = 1; i < result.length; i++){
result[i] = ' ' + result[i]
}
return result.join('\n');
}).join('\n').slice(0,-1) + ' ]';
}
}
function change(){
let array = JSON.parse(document.getElementById('input').value);
let output = document.getElementById('output');
output.innerText = recursivePrettyPrint(array);
}
<textarea id=input></textarea>
<button id=run onclick=change()>Pretty Print</button>
<pre id=output></pre>
Numbers may be multiple digits. The input will always be orthogonal/rectangular, and you may take its dimensions as well. Trailing spaces on lines are allowed.
Testcases
[[892, 759], [962, 251]] ->
[ [892, 759],
[962, 251] ]
[118, 922, 619] ->
[118, 922, 619]
[[966, 639, 616, 255], [622, 483, 87, 241], [453, 870, 728, 725], [163, 936, 48, 967], [261, 833, 87, 200]] ->
[ [966, 639, 616, 255],
[622, 483, 87, 241],
[453, 870, 728, 725],
[163, 936, 48, 967],
[261, 833, 87, 200] ]
[[[[[912, 547], [366, 754]], [[723, 536], [779, 238]]], [[[559, 392], [602, 709]], [[692, 915], [412, 302]]]], [[[[3, 504], [936, 83]], [[352, 442], [425, 375]]], [[[380, 440], [793, 762]], [[850, 321], [780, 457]]]]] ->
[ [ [ [ [912, 547],
[366, 754] ],
[ [723, 536],
[779, 238] ] ],
[ [ [559, 392],
[602, 709] ],
[ [692, 915],
[412, 302] ] ] ],
[ [ [ [3, 504],
[936, 83] ],
[ [352, 442],
[425, 375] ] ],
[ [ [380, 440],
[793, 762] ],
[ [850, 321],
[780, 457] ] ] ] ]
[[[128, 910, 664, 658], [172, 238, 564, 492], [325, 384, 566, 90]], [[876, 819, 764, 105], [583, 528, 731, 839], [480, 126, 692, 875]], [[215, 84, 268, 504], [400, 674, 997, 526], [799, 692, 193, 296]], [[943, 185, 567, 188], [118, 200, 879, 409], [116, 493, 62, 343]]] ->
[ [ [128, 910, 664, 658],
[172, 238, 564, 492],
[325, 384, 566, 90] ],
[ [876, 819, 764, 105],
[583, 528, 731, 839],
[480, 126, 692, 875] ],
[ [215, 84, 268, 504],
[400, 674, 997, 526],
[799, 692, 193, 296] ],
[ [943, 185, 567, 188],
[118, 200, 879, 409],
[116, 493, 62, 343] ] ]