Skip to main content
1 of 3

Bash + GNU utilities, 117 bytes

n=$[$1**$1] seq -f$1o%.fd$n+1-p $n|dc|rev|sed -r "s/(0+|$[$1-1]*).*$/\1/;s/^(0*)/\1$1/;s/^1/[1]/"|tr \\n0$[$1-1] \ [] 

Try it online!


The program counts from 0 to n^n-1 in base n, where n is the input.

For each base-n number k in the count, it does the following:

  1. If k ends with at least one digit 0, print a '[' for each digit 0 at the end of k.
  2. Print n.
  3. If k ends with at least one digit n-1, print a ']' for each digit n-1 at the end of k.

(The value n=1 needs to have brackets added as a special case.)

Maybe there's a shorter way to implement this idea.


Sample run:

./array 3 [[[3 3 3] [3 3 3] [3 3 3]] [[3 3 3] [3 3 3] [3 3 3]] [[3 3 3] [3 3 3] [3 3 3]]]