21
\$\begingroup\$

Task

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",
  "gift"],

 ["goes",
  "here"]]

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

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

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

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

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.
\$\endgroup\$
  • \$\begingroup\$ Couldn't you save a byte with h=.,,[ \$\endgroup\$ – Adám Dec 20 '16 at 14:27
4
\$\begingroup\$

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

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

note: previous answer assumed default padval

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

Python, 106 104 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!

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

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.
\$\endgroup\$
3
\$\begingroup\$

Dyalog APL, 31 19 13 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!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 34 33 31 bytes

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

Try it online! (string) or Try it online! (numerical)

\$\endgroup\$
1
\$\begingroup\$

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? :-)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.