# Build me a room

Related

A room, in the context of this challenge, is a multidimensional array where the elements on the "outside" are 1, to represent the walls, and all the other elements are 0 (empty space inside the room)

Here's a 1D room with size 5:

[1,0,0,0,1]


And here's a 2D room with size 6x4:

[[1,1,1,1],
[1,0,0,1],
[1,0,0,1],
[1,0,0,1],
[1,0,0,1],
[1,1,1,1]]


It's 6x4 and not 4x6 because the list has length 6 at depth 1, and length 4 at depth 2.

Or a 4x4x4 room (imagine each 4x4 sub-array as a 2D slice of the 3D room):

[[[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[1,1,1,1]],
[[1,1,1,1],
[1,0,0,1],
[1,0,0,1],
[1,1,1,1]],
[[1,1,1,1],
[1,0,0,1],
[1,0,0,1],
[1,1,1,1]],
[[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[1,1,1,1]]]


A room can be recursively defined by starting with 0 and replacing each 0 with [1,0,0,...,0,1] and each 1 with [1,1,...,1,1], each to the appropriate length and depth.

Your challenge is to take a list of integers and output a room with those dimensions. Dimensions will always be $$\ >1 \$$. A dimension value of 2 means no space inside, so if there's a 2 the whole thing will be 1s.

You may use any two consistent values instead of 0 and 1 to represent space and wall.

Output may be as a flattened string, e.g. [3,4] => 111110011111.

You may take the coordinate list reversed (inside out).

# Scoring

This is , so the shortest code in bytes wins!

# Testcases

[3] => [1,0,1]
[2,2,2,2,2] => [ [ [ [ [1,1], [1,1] ], [ [1,1], [1,1] ] ], [ [ [1,1], [1,1] ], [ [1,1], [1,1] ] ] ], [ [ [ [1,1], [1,1] ], [ [1,1], [1,1] ] ], [ [ [1,1], [1,1] ], [ [1,1], [1,1] ] ] ] ]
[4,4,4] => [ [ [1,1,1,1], [1,1,1,1], [1,1,1,1], [1,1,1,1] ], [ [1,1,1,1], [1,0,0,1], [1,0,0,1], [1,1,1,1] ], [ [1,1,1,1], [1,0,0,1], [1,0,0,1], [1,1,1,1] ], [ [1,1,1,1], [1,1,1,1], [1,1,1,1], [1,1,1,1] ] ]
[5,6] => [ [1,1,1,1,1,1], [1,0,0,0,0,1], [1,0,0,0,0,1], [1,0,0,0,0,1], [1,1,1,1,1,1] ]
[3,19] => [ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ]
[12] => [1,0,0,0,0,0,0,0,0,0,0,1]


Thanks to golden_bat and pxeger for clarifying and fixing this challenge.

• May the input list be taken in reverse order? Aug 3 at 9:57
• @Arnauld Sure. Will add Aug 3 at 10:21
• Isn't 111110011111 the result of [4,3] rather than [3,4]? Aug 3 at 12:26
• Shouldn't test case [19,3] be [3,19]? Aug 3 at 14:00
• Test case [2,2,2,2] doesn't look quite right - shouldn't it have 4 levels of nesting, not 5? Aug 3 at 14:06

# Jelly, 7 bytes

R%)Ịoþ/


A monadic Link that accepts the dimensions as a list of positive integers ordered by depth descending (like the examples in the question) and yields the room as a multi-dimensional list of 1s and 0s.

Try it online! Or see the test-suite

### How?

R%)Ịoþ/ - Link: list of positive integers, Sizes
)     - for each (size in Sizes):
R       -   range (size)  -> [1,2,3,...,size]
%      -   modulo (size) -> [1,2,3,...,0]
Ị    - insignificant?  -> [...,[1,0,0,...,1],...]
/ - reduce by:
þ  -   make a table using:
o   -     logical OR (vectorises)


# APL(Dyalog Unicode), 11 bytes SBCS

⊢↑1∘-↑-∘2⍴≡


Try it on APLgolf!

Sometimes the most straightforward method wins. Space is 1, wall is 0.

### How it works

⊢↑1∘-↑-∘2⍴≡    Monadic train; ⍵←dimension vector
-∘2⍴≡    Create the core of ones of dimensions of ⍵-2
1∘-↑         Prepend a layer of zeroes by overtaking ⍵-1 in reverse
⊢↑             Append a layer of zeroes by overtaking ⍵


# APL (Dyalog Unicode), 12 bytes

Anonymous tacit prefix function.

⊂(∨/=,1∊⊢)¨⍳


Try it online!

⊂()¨⍳ apply the following tacit infix function between the entire argument and each index in an array of those dimensions:

1∊⊢ is there a 1 in the index?

=, prepend a Boolean list indicating which coordinate are equal to the corresponding element of the original argument

∨/ are any true? (lit. OR reduction)

# JavaScript (ES6), 65 bytes

Expects the input list in reverse order.

Very similar to the 66-byte version, but returns a flattened string.

f=([n,...a],b='0')=>n?b.replace(/./g,v=>f(a,1+v.repeat(n-2)+1)):b


Try it online!

# JavaScript (ES6), 66 bytes

Expects the input list in reverse order.

f=([n,...a],b=[0])=>n?b.map(v=>f(a,[1,...Array(n-2).fill(v),1])):b


Try it online!

### Commented

f = (                   // f is a recursive function taking:
[ n,                  //   n = next value from the input list
...a ],          //   a[] = all remaining values
b = [ 0 ]             //   b[] = output, initialized to [ 0 ]
) =>                    //
n ?                     // if n is defined:
b.map(v =>            //   for each value v in b[]:
f(                  //     do a recursive call:
a,                //       pass the remaining values
[                 //       build an array consisting of:
...Array(n - 2) //         followed by n - 2 values
.fill(v),       //         set to v
1               //         followed by a trailing '1'
]                 //       end of array
)                   //     end of recursive call
)                     //   end of map()
:                       // else:
b                     //   we're done: return b[]


# K (ngn/k), 14 11 bytes

|/~&/|:'\!:


Try it online!

@ngn suggested a 13-byter {|/~x&|'x:!x} in chat, which

• overwrites x with the odometer (x:!x),
• takes element-wise minimum & with its row-wise reverse |'x,
• and then does |/~ as below.

The 11-byter above is the result of x f g x trainifying tip. 1 in 1|:'\ can be omitted because odometer's reverse is always different from itself (assuming all dimensions are at least 2).

# K (ngn/k), 14 bytes

{|/~(x-1)!'!x}


Try it online!

Same bytecount as Traws's but different approach. Returns a flattened list of ones on the wall and zeros inside. The helper function g reshapes the flattened list into the desired shape.

### How it works

{|/~(x-1)!'!x}    monadic function; x: a list
!x     odometer: column-wise list of coordinates in x-shaped array
(x-1)!'       use modulo x-1 to convert occurrence of x-1 in each row to zeros
~              boolean NOT
|/               reduce by boolean OR over each column


s[]="0"
s(a:b)|k<-[1..product b]>>"1"=k++([3..a]>>s b)++k


Try it online!

Builds a flat list since Haskell is strongly typed. Also takes the coordinates in reverse.

# Python 3.6, 63 bytes

f=lambda d,w=0:d and[f(d[1:],x)for x in[1,*[w]*(d[0]-2),1]]or w


Try it online!

Got the and/or mechanism from aeh5040's post. Originally I was just using a ternary operator.

• Welcome to Code Golf, and nice first answer! Be sure to check out our Tips for golfing in Python page for ways you can golf your program. I've added a link to Try it online, so that other users can test your program Aug 3 at 19:48
• A rather obscure trick: [w]*(d[0]-2) -> -2%d[0]*[w]. Aug 3 at 20:57
• ah thanks dingledooper. I thought there might be a way I could change the order of operations in order to avoid the parentheses but I couldn't come up with this. Aug 4 at 1:11

# Octave with Image Package, 25 bytes

@(d)padarray(e(d-2),+~~d)


Anonymous function that inputs a vector with the coordinates and outputs an n-dimensional array. Wall and space are respectively 0 and e (equal to 2.71828...)

Note that, when displaying, the first dimension corresponds to the vertical direction.

Try it online!

### Explanation

@(d)padarray(e(d-2),+~~d)

@(d)                      % Define anonymous function
e(d-2)       % N-dim array containing e, with side lengths given by d-2
+~~d  % Negate d twice, cast to double: gives vector of N ones (**)
padarray(      ,    ) % Add a frame of zeros to (*) with thickness (**) in each dim


# Python 3.8 (pre-release), 65 bytes

f=lambda d,o=0:d and[f(t:=d[1:],1),*[f(t,o)]*(d[0]-2),f(t,1)]or o


Try it online!

{.-@<:{.-&2$1:  Try it online! This is a translation of Bubbler's nice answer into J. I tried a few other approaches, including stumbling on the same one Adam used, but in J Bubbler's approach was easily the shortest. # K (ngn/k), 14 bytes |/:/{~x#!x-1}'  Try it online!  { }' for each dimension apply the function in lambda x#!x-1 overtake n from the range of n-1, e.g. 0 1 2 3 4 0 for n=6 ~ "not" the array to get ones at the edges e.g. 1 0 0 0 0 1 |/:/ max table along all dimensions  # Jelly, 10 bytes Œp’ỊƇƇo⁸ŒṬ  Try it online! A monadic link taking a list of integers and returning a list of the appropriate depth of 0s and 1s. Works by generating a list of all of the coordinates, keeping only those that are the walls and using the multidimensional untruthy link to generate the final list. # Coconut, 68 bytes Uses the recursive method described in the challenge. Takes input in reversed order. g=(x,k)->x*0==0and[1,*[x]*(k-2),1]or[*map(g$(?,k),x)]
reduce\$(g,?,0)


Try it online!

map(elem 0).mapM(\i->0:[2-i..0])


Try it online!

Outputs a flat list of Booleans

# Julia 0.7, 39 37 bytes

x->(a=ones(x);a[(:).(2,x.-1)...]=0;a)


Try it online!

Takes input as a tuple of dimensions, outputs a multidimensional array with 1s and 0s formatted as floats.

# Charcoal, 28 bytes

≔0ζ≔1ηＦ⮌θ«≔Ｅι⎇﹪κ⊖ιζηζ≔Ｅιηη»ζ


Try it online! Link is to verbose version of code. Outputs vertically with ever larger number of newlines as delimiters between different dimensions. Explanation:

≔0ζ≔1η


Start with 0 as the hollow room and 1 as a solid room.

Ｆ⮌θ«


Loop through the dimensions in reverse order so that the innermost dimension is processed first.

≔Ｅι⎇﹪κ⊖ιζηζ


For the next level of hollow room, take the number of rooms and make the first and last solid.

≔Ｅιηη


The next level of solid rooms is just an array of the appropriate number of solid rooms of the previous level.

»ζ


Output the final hollow room.

# APL (Dyalog Unicode), 10 bytes

∨∘⌽⍨∘,0∊¨⍳


Try it online!

• It doesn't work for 5,6 and 19,3. Aug 4 at 7:52
• @Bubbler thanks. i was reshaping the result to the reversed argument. instead, i should have been reshaping to the actual argument and then transposing. this is just for visualization, the actual result is flat.
– ngn
Aug 4 at 7:57
• Oh, so that was a formatting issue. Didn't think of flatten (ravel) to reverse the entire array easily. Aug 4 at 8:02

# 05AB1E, 13 bytes

ÎvTyo<y<o>‚b‡


Straight-forward approach. Can definitely be golfed a bit with a smarter approach.
Output is a flattened string.

Explanation:

Î           # Push 0 and the input-list
v          # Loop over the integers y of this list:
T         #  Push 10
yo<      #  Push 2^y-1 (oeis sequence A000225)
y<o>     #  Push 2^(y-1)+1 (oeis sequence A000051)
‚    #  Pair them together
b   #  Convert both to a binary string
‡  #  Transliterate the [1,0] to ["111...111","100...001"] respectively
# (after which the result is output implicitly)