# N-dimensional N^N array filled with N

In: Enough memory and a positive integer N

Out: N-dimensional N^N array filled with N, where N^N means N terms of N-by-N-by-N-by...

Examples:

1: [1] which is a 1D array (a list) of length 1, containing a single 1

2: [[2,2],[2,2]] which is a 2D array (a table) with 2 rows and 2 columns, filled with 2s

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]]] which is a 3D array (a cube) with 3 layers, 3 rows, and 3 columns, filled with 3s

4: [[[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]],[[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]],[[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]],[[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]],[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]]] 

5 and 6: Please see one of the answers.

• If our language does not support arrays, what would be an acceptable output format?
– Okx
Feb 28 '17 at 16:54
• Since "Enough memory" is part of the input, I want to see an answer that controls a robot to actually take the memory as input and plug it in before using it. Feb 28 '17 at 19:05
• Do all the arrays need to be distinct objects?
– Neil
Mar 1 '17 at 0:25
• @user2357112 I think that's more of a precondition type issue. I doubt the op actually expects the function to accept memory as input. Mar 1 '17 at 4:31
• @TheGreatDuck Correct, but I'm pretty sure user2357112 meant it as a joke.
– Adám
Mar 1 '17 at 5:47

# 05AB1E, 2 bytes

Fи


: Uses a Bash script to execute the 05AB1E code, so it'll use the (implicit) input within the test suite. If the number would be on the stack instead of as input, it won't work for $$\n=2\$$ (no idea why..): see this traditional test suite, where every output is correct except for $$\n=2\$$.

Explanation:

F   # Loop the (implicit) input amount of times:
и  #  Repeat the (implicit) input the current values amount of times (as list)
# (after which the resulting nested list is output implicitly)


try it online!

{[[xx] [xx] $_ xx 3]}  • Welcome to Code Golf! Nice first answer. Aug 29 at 1:52 # Julia 0.6, 23 21 bytes n->n>(n>n)... > =fill  Try it online! (Thanks to @MarcMush for -2 bytes.) In Julia, binary operators are parsed as function calls, so a>b is >(a, b), which here is fill(a, b). So the above is really saying: n->fill(n,fill(n,n)...)  The first argument to fill is the value to fill the new array with. The following arguments to that are the dimensions of the new array. Here we create those dimensions themselves using another fill call: we create an array of n ns, then splat that with ... so that the first fill  call gets n number of dimension arguments, each with value n - so it creates an n-dimensional array where each dimension length is n, and filled with the value n. • 21 bytes I'm surprised this works Aug 27 at 9:07 • I was wondering why @MarcMush 's improved version doesn't work if we used + or * instead of >, which lead to a Zulip discussion, and a Github issue from that. (The answer has to do with precedence btw, of > vs ... vs +,*,etc.) Sep 3 at 14:41 # Perl 6, 61 bytes my$x=prompt(0);my @a=$x xx$x;"@a=[@a] xx $x;".EVAL xx$x-1;


Big rippof from the Python 2 answer, but converted :P

• Instead of my $x=prompt(0);, you can use $_=get;. You also need to print or return the result somehow, but note that the rules of this site allow writing answers as functions or lambdas instead of of full programs, which is usually shorter in Perl 6.
– smls
Feb 28 '17 at 22:54

# Clojure, 63 bytes

#(loop[a(repeat % %)d 1](if(= d %)a(recur(repeat % a)(inc d))))


This is a lambda function, usage is like so:

(#(...) {input_no})


...where {input_no} is replaced with the number.

Output for 3 is like this:

(((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)))


This uses Clojure's definition of lists, which are denoted as ().

## Ungolfed code and explanation:

; Defines the function
(defn layered [n]
; Begins a loop with a variable depth of 1,
; and a list of n elements which are all n
(loop [depth 1
array (repeat n n)]
; If "depth" is equal to n, return the list
(if (= depth n) array
; Else, continue on with the loop, with
; an incremented "depth"...
(recur (inc depth)
; ...and a list which contains the
; list repeated n times
(repeat n array)))))


# I, 7 bytes

I got this from my colleague, the creator of I.

#Bbhph~


#Bb     the copy # function Bound to binding
   hp  hook the argument to (the right of) the power function (repeat)
     h~hook the argument to the left ~ (of the entire resulting function)

Try it online!

# SNOBOL4 (CSNOBOL4), 59 bytes

	DEFINE('F(N)')
F	F =ARRAY(DUPL(N ',',N - 1) N,N)	:(RETURN)


Try it online!

Defines a function that returns an ARRAY with the appropriate dimensions and filled with value N.

# Haskell, 51 bytes

Other than the existing Haskell solutions this constructs a usable data type not just a string representation thereof (and is shorter):

data L=N[L]|E Int
f n=iterate(N.(<$[1..n]))(E n)!!n  Try it online! ### Explanation / Ungolfed The reason why this is an interesting challenge in Haskell is that a solution to this challenge needs to return different deeply nested lists and Haskell (without importing external modules) does not support this. One workaround which turned out to be the golfiest, is to define our own data type: data NestedList = Nest [NestedList] | Entry Int  Now we can just define a function f :: Int -> NestedList which is able to represent all the required data types: pow n = iterate (Nest . replicate n) (Entry n) !! n  # Japt, 14 bytes _òU}g[UpU ÆUÃ]  Try it online! Output is wrapped in an extra singleton array. Explanation:  Æ Ã #Create an array U #Where every element is U UpU #And the length is U^U _ }g[ ] #Repeat this function U times: òU # Cut into slices of length U  • 14 bytes; I was hoping it'd be shorter. Feb 22 '19 at 8:34 # Python 3, 89 Bytes def f(x): r=[x for _ in range(x)] for _ in range(x-1):r=[r for _ in range(x)] print(r)  ## Lithp, 82 bytes (def f #N::((def i #N,I,K::((if(> I 0)((i N(- I 1)(list-fill N K)))K)))(i N N N)))  Try it online! I tried implementing this as a list comprehension, but couldn't comprehend how to do it correctly. Instead I went for an implementation based on the JavaScript answer. Unfortunately, my language is fairly long-winded, and this is complicated by the fact that I lack shorthand and other useful features. A more readable version is available at the Try it Online link. ## Hoon, 56 bytes |= n/@ =+ i=1 |- ?: =(n i) (reap n n) (reap n$(i +(i)))


Create a new function that takes an atom n. Make a variable i starting at 1, and start a loop: if i==n return a list with n elements of n, else return a list with n elements of the value returned by recursing to the start of the loop with i = i + 1.

I'm a little bit upset that there's not really anything you can do to golf this in Hoon :/ The standard trick of using unnamed variables doesn't apply because it's longer to use lark syntax for once, due to the loop shifting the location of i.

> =f |=
n/@
=+
i=1
|-
?:
=(n i)
(reap n n)
(reap n $(i +(i))) > (f 1) ~[1] > (f 2) ~[~[2 2] ~[2 2]] > (f 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]]] > (f 4) ~[ ~[ ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ] ~[ ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ] ~[ ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ] ~[ ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ~[~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4] ~[4 4 4 4]] ] ]  # WendyScript, 34 bytes #:(x){<<j=[x]*x#i:1->x j=[j]*x/>j} #:(x){<<j=[x]*x#i:1->x j=[j]*x/>j}(2) // [[2, 2], [2, 2]]  Try it online! • Is it not possible to have the link actually populate with the your code? – Adám Aug 21 '17 at 19:46 • I will have to add that functionality, I'll get back to you in 30 minutes but that's a good point. Aug 21 '17 at 19:47 • @Adám updated link Aug 21 '17 at 21:36 # 12-basic, 42 bytes A=[N=INPUT()]*N FOR I=2TO N A=[A]*N NEXT?A  # V, 14 bytes é,"aPÀñDÀpys0]  Try it online! input é, # write comma "aP # paste register A (input) Àñ # loop À (number in register A) times D # cut to end of line Àp # paste À times ysB] # surround everything before comma in square brackets  • This prints a trailing comma.. Jun 30 '18 at 21:30 • @BMO is the trailing comma a problem? it was never explicitly stated that it wasn't allowed. if it is a problem I can change it, it just adds one or two characters Jul 1 '18 at 21:59 # C (GCC), 53 bytes Since C "has" multidimensional arrays (i.e. chunks of memory plus automatic offset calculation), this is almost a reasonable solution. Still, it's mostly a joke and borders on non-competing. f(n){int s=pow(n,n),*p=malloc(s*8);wmemset(p,n,s);}  Try It Online In the TIO I go through the ceremony of casting the result (which is an int) to the appropriate array type despite the fact that my array print function (appropriately) takes a generic pointer. While I can't demonstrate that the structure of the array is determined by f (it's not), I do include a flat printout of the elements of the output for an input of 3 using the multidimensional array element access syntax, to show that the layout of the returned memory matches that of the corresponding array type. ## Byte count • function: 51 bytes • compiler flags (-lm): 2 bytes ## Unportability • The result pointer is returned through an int, so this may not work for all heap addresses. • By using wmemset I require that the sizes of int and wchar_t are equal, and the scaling in the argument to malloc requires the sizes to be at most 8 bytes. • Implicit declaration madness. I don't even know why passing ints to an unprototyped pow that actually takes doubles seems to work correctly. • The function "returns" by (I think) just leaving the return value of wmemset in the proper register. This behavior may be particular to the x86 architecture. • 42 bytes s;f(n){wmemset(malloc(4*s),n,s=pow(n,n));} Jun 24 '18 at 8:12 • @ceilingcat Nice. But as far as I know neither C nor GCC defines the order of argument evaluation--what guarantees that will work? Jun 24 '18 at 16:36 # Pip-p, 12 bytes YaLaY[y]RLay  Try it online! ### Explanation YaLaY[y]RLay Y Yank into the y variable a the first command-line argument, a La Fixed-iterations loop, do the following a times: [y] y wrapped in a list RLa repeated a times (results in a list containing a copies of y) Y Yank that list back into y y After the loop, print y  # Factor + arrays.shaped, 39 bytes [ 3 dupn <array> swap repeated-shaped ]  Try it online! repeated-shaped Takes a sequence of dimensions (i.e. a shape) and an element and creates an array from them. The rest of the code turns 4 into { 4 4 4 4 } 4, for example. # Jelly, 3 bytes ḷⱮ¡  Try it online! Like Lynn's solution which heavily inspired this, a full program only.  ¡ Repeat [input] times, starting with the input on the left and right: Ɱ Produce an array containing, for every element of the right argument, ḷ the left argument.  Although dyadic ¡, rather than reusing its right argument, uses the previous left argument for each successive iteration, in this case either behavior would do the same thing--since each intermediate result has the same length as the (implicitly rangified) initial input. ## Perl 5, 34 bytes sub{eval'@_=[(@_)x$x];'x($x="@_")}  Try it online! # Lua, 85 bytes function f(n,d,x)d=d or 2 x={}for i=1,n do x[i]=d>n and n or f(n,d+1)end return x end  Try it online! (The TIO link contains a bit of extra code to print the table as Lua has no built-in way of printing a table's contents) ## Explanation function f(n, d, x) d = d or 2 x = {} for i = 1, n do x[i] = d > n and n or f(n, d + 1) end return x end  As you can probably see, f is a recursive function which takes the number N as n, and two other arguments, d and x. d is the depth, and is not used in the initial call, in which case it defaults to 2. x is not actually used as a parameter, but is there because the function requires a temporary local variable, and it is shorter to declare it as a parameter than to use the local keyword. # Zsh, 74 bytes try it online! f(){for i ({1..$1})z+=($2);<<<($z)}
for j ({1..$1})k=f$1 ${k:-$1};<<<$k  Zsh doesn't have multi dimensional arrays but it can do arrays of strings. Here we build up string $k by plugging it into f, then taking that result as the new $k, iterating N times ($1).