Wrap a seasonal present

Given a wrapper element and a non-jagged 3D array, wrap the array top, bottom, and all-around. You must handle both character and numeric data, but the wrapper and the present will have the same data type.

Character example

For character data, you may chose to handle either 3D arrays of single characters or 2D arrays of strings:

Given the 2 layer, 2 row, 4 column character array

[[["Y","o","u","r"],
["g","i","f","t"]],

[["g","o","e","s"],
["h","e","r","e"]]]


and the character ".", answer the 4 layer, 4 row, 6 column character array

[[[".",".",".",".",".","."],
[".",".",".",".",".","."],
[".",".",".",".",".","."],
[".",".",".",".",".","."]],

[[".",".",".",".",".","."],
[".","Y","o","u","r","."],
[".","g","i","f","t","."],
[".",".",".",".",".","."]],

[[".",".",".",".",".","."],
[".","g","o","e","s","."],
[".","h","e","r","e","."],
[".",".",".",".",".","."]],

[[".",".",".",".",".","."],
[".",".",".",".",".","."],
[".",".",".",".",".","."],
[".",".",".",".",".","."]]]


or given the 2 row, 2 column array of 4-character strings

[["Your",

["goes",
"here"]]


and the character ".", answer the 4 row, 4 column array of 6-character strings

[["......",
"......",
"......",
"......"],

["......",
".Your.",
"......"],

["......",
".goes.",
".here.",
"......"],

["......",
"......",
"......",
"......"]]


Numeric example

Given the 2 layer, 2 row, 2 column numeric array

[[[1,2],
[3,4]],

[[5,6],
[7,8]]]


and the number 0, answer the 4 layer, 4 row, 4 column numeric array

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

[[0,0,0,0],
[0,1,2,0],
[0,3,4,0],
[0,0,0,0]],

[[0,0,0,0],
[0,5,6,0],
[0,7,8,0],
[0,0,0,0]],

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

• Can we assume the length of each "gift element" to wrap is identical? – XavCo7 Dec 20 '16 at 14:25
• @XavCo7 Yes you can. – Adám Dec 20 '16 at 14:28
• Related. – Martin Ender Dec 20 '16 at 14:51
• What kind of output is acceptable. Need it be a datastructure of a three d array or is textual output acceptable – Rohan Jhunjhunwala Dec 20 '16 at 17:07
• @RohanJhunjhunwala You may freely choose representation, but input and output formats must be the same. – Adám Dec 20 '16 at 17:44

J, 16 15 bytes

[h"2[h"1 h=.,,[


This is an anonymous verb. Try it online!

Thanks to Adám for 1 byte!

Explanation

[h"2[h"1 h=.,,[  Wrapper is x, present is y.
,    Prepend x to y
,   then append
[  x.
This gives x y x, and the wrapper automatically spreads to form 2D slices.
h=.     Save the above operation (not its result) to h.
[h"1         Apply h to x and every 2D slice of the previous result.
[h"2             Apply h to x and every 1D slice of the result of that.

• Couldn't you save a byte with h=.,,[ – Adám Dec 20 '16 at 14:27

JavaScript (ES6), 97 bytes

(a,e)=>[c=[,,...b=a[0]].fill(d=[,,...b[0]].fill(e)),...a.map(a=>[d,...a.map(a=>[e,...a,e]),d]),c]


Where a is the three-dimensional array and e is the wrapper. Automatically converts a two-dimensional array of strings to a three-dimensional array of characters. Alternative version for when a is a two-dimensional array of strings and e is a character and you want to return a two-dimensional array of strings:

(a,e)=>[c=[,,...a[0]].fill(d=e.repeat(a[0][0].length+2)),...a.map(b=>[c,...b.map(s=>e+s+e),d]),c]

• Looks like this fails on numeric e. – Adám Dec 20 '16 at 21:20
• @Adám Ah, sorry, I may have misunderstood the question. – Neil Dec 20 '16 at 21:35
• @Adám New version, fortunately same byte count, works on three-dimensional arrays of arbitrary elements (will autoconvert strings to character arrays). – Neil Dec 21 '16 at 1:11

Octave, 23 27 bytes

@(a,p)padarray(a,[1 1 1],p)


array: a
padval: p

It can be called as :

(@(a,p)padarray(a,[1 1 1],p))([1 2;3 4],40)


try (paste!)it on Octave Online

• This looks like it only accepts a single argument (the array). Where does it get the wrapping character/number from? – smls Dec 20 '16 at 20:57
• by default the function pad by 0 – rahnema1 Dec 20 '16 at 20:59
• @rahnema1 So what if the padding is 42, or "Z"? – Adám Dec 20 '16 at 21:17
• Oh...answer updated including PADVAL – rahnema1 Dec 20 '16 at 21:23
• +1 I'm surprised that there is a built-in for exactly this. – Adám Dec 20 '16 at 21:29

Python, 106104 126 bytes

def w(g,c):x=len(g[0][0])+2;k=[[c*x]*(len(g[0])+2)];return k+[[c*x,*[c+"".join(str(k)for k in j)+c for j in i],c*x]for i in g]+k


Called as w(gift, wrapping character). Can use the string and the array notation. Try it online!

• I couldn't figure out how to run this on repl.it. Can you create a link? – Adám Dec 20 '16 at 21:30
• @Adám repl.it/Eu4M/1 – TidB Dec 20 '16 at 21:34
• Thanks. Looks like numeric fails. – Adám Dec 20 '16 at 21:39
• @Adám Gotcha, I misinterpreted (hehe) the specification. Slightly longer now, but it actually works correctly > repl.it/Eu4M/4 – TidB Dec 20 '16 at 21:52
• Wow, now you went beyond specs, you didn't need to handle the mixed data types case. – Adám Dec 20 '16 at 21:59

Perl 6, 86 bytes

->\a,\w{my @z=[[w xx a[0;0]+2]xx a[0]+2]xx a+2;@z[1..a;1..a[0];1..a[0;0]]=a[*;*;*];@z}


A lambda that takes the 3D array and wrapping character as arguments.

• It first creates a 3D output array of the correct size, filled with the wrapping character.
• Then it uses the array slice syntax to assign the values of the original array into the correct slots of the new array, in one fell swoop.

Dyalog APL, 311913 12 bytes

Almost a transliteration (31 bytes) of @Zgarb's solution.

An anonymous function. Left argument is wrapping, right argument is gift.

⊣h⍤1⊣h⍤2(h←⍪⍪⊣)


⊣h⍤1 h applied, with the anonymous function's left argument, to the columns of

⊣h⍤2 h applied, with the anonymous function's left argument, to the rows of

h← h applied to the major cells i.e. the layers of the anonymous function's arguments, where h is

⍪ the left argument prepended to the right argument

⍪ prepended to

⊣ the left argument

In other words, h is a function which surrounds its right argument (the gift) with its left argument (the wrapper). h is then applied to the gift's layers, then the rows of that, and finally the columns of that.

TryAPL online!

This Dyalog APL version 16.0 solution (19 bytes – courtesy of @ngn) handles any number of dimensions:

{⍵@(1+⍳⍴⍵)⊢⍺⍴⍨2+⍴⍵}


⍵ the gift

@( placed at

1+ one plus

⍳ all the indices of

⍴⍵ the shape of the gift

)⊢ in the array consisting of

⍺⍴⍨ the wrapper reshaped to the shape

2+ two added to

⍴⍵ the shape of the gift

In other words, we create an array entirely of wrapper elements, which in every dimension is two elements larger than the gift, then we place the gift into that array (thus replacing the wrapping elements in those positions) at an offset of one from the edges, i.e. in the center.

My own invention (-1 thanks to @ngn):

(⌽2 3 1⍉,)⍣6


This applies an anonymous function-train 6 times, each time with the wrapper as left argument, and the result of the previous application as right argument (although the first time around it will be the unmodified gift):

( an anonymous function-train

⌽ reverse columns of

2 3 1⍉ the rows-to-layers, columns-to-rows, layers-to-columns transposition of

, the wrapper followed by the gift

)⍣6 applied six times

In other words, we add a layer of wrapper on the top of the array, then warp it so that the next side gets rotated into the top layer position, ready for another round of wrapping. This is repeated six times, with the final warping repositioning all axes to the original order.

TryAPL online!

05AB1E, 3433 31 bytes

vy¬gÌ²s×S©svy².ø¼}®)ˆ}¾F®})¯s.ø


Ruby, 89 bytes

->a,b{(w=[[z=b*2+a[0][0].tr('^|',b)]*(2+a[0].size)])+a.map{|x|[z]+x.map{|y|b+y+b}+[z]}+w}
`

Have I ever told you I'm only here to learn ruby? :-)