Wrap a seasonal present

21

1

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]]]

Adám

Posted 2016-12-20T13:40:57.377

Reputation: 37 779

Can we assume the length of each "gift element" to wrap is identical? – XavCo7 – 2016-12-20T14:25:50.883

@XavCo7 Yes you can. – Adám – 2016-12-20T14:28:07.867

Related. – Martin Ender – 2016-12-20T14:51:29.313

What kind of output is acceptable. Need it be a datastructure of a three d array or is textual output acceptable – Rohan Jhunjhunwala – 2016-12-20T17:07:09.050

@RohanJhunjhunwala You may freely choose representation, but input and output formats must be the same. – Adám – 2016-12-20T17:44:41.563

@Adám Congrats on 10k!! – Luis Mendo – 2016-12-20T19:51:53.507

@LuisMendo Thanks. I guess that'll be by seasonal present from SE :-) – Adám – 2016-12-20T20:29:40.610

In the example of 3D char arrays, the wrapped gift says 'Your gift/goea here', do we need to edit the string? – boboquack – 2016-12-21T00:25:29.517

Answers

3

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!

Adám

Posted 2016-12-20T13:40:57.377

Reputation: 37 779

8

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.

Zgarb

Posted 2016-12-20T13:40:57.377

Reputation: 39 083

Couldn't you save a byte with h=.,,[ – Adám – 2016-12-20T14:27:40.960

4

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]

Neil

Posted 2016-12-20T13:40:57.377

Reputation: 95 035

Looks like this fails on numeric e. – Adám – 2016-12-20T21:20:22.100

@Adám Ah, sorry, I may have misunderstood the question. – Neil – 2016-12-20T21:35:33.433

@Adám New version, fortunately same byte count, works on three-dimensional arrays of arbitrary elements (will autoconvert strings to character arrays). – Neil – 2016-12-21T01:11:44.927

3

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!

TidB

Posted 2016-12-20T13:40:57.377

Reputation: 561

I couldn't figure out how to run this on repl.it. Can you create a link? – Adám – 2016-12-20T21:30:21.880

@Adám https://repl.it/Eu4M/1

– TidB – 2016-12-20T21:34:24.540

Thanks. Looks like numeric fails.

– Adám – 2016-12-20T21:39:35.390

@Adám Gotcha, I misinterpreted (hehe) the specification. Slightly longer now, but it actually works correctly > https://repl.it/Eu4M/4

– TidB – 2016-12-20T21:52:38.643

Wow, now you went beyond specs, you didn't need to handle the mixed data types case. – Adám – 2016-12-20T21:59:31.340

3

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

rahnema1

Posted 2016-12-20T13:40:57.377

Reputation: 5 435

This looks like it only accepts a single argument (the array). Where does it get the wrapping character/number from? – smls – 2016-12-20T20:57:24.293

by default the function pad by 0 – rahnema1 – 2016-12-20T20:59:12.307

@rahnema1 So what if the padding is 42, or "Z"? – Adám – 2016-12-20T21:17:19.497

Oh...answer updated including PADVAL – rahnema1 – 2016-12-20T21:23:38.227

1+1 I'm surprised that there is a built-in for exactly this. – Adám – 2016-12-20T21:29:43.987

3

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.

smls

Posted 2016-12-20T13:40:57.377

Reputation: 4 352

1

05AB1E, 34 33 31 bytes

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

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

Emigna

Posted 2016-12-20T13:40:57.377

Reputation: 50 798

1

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

G B

Posted 2016-12-20T13:40:57.377

Reputation: 11 099