6
\$\begingroup\$

This challenge is specific to languages of the APL family. Although it might be solvable in other languages, please do not expect this to be easy as it depends on some specific concepts of this language family.

Many array oriented languages have the concept of a box (also called enclosure). A box encapsulates an array, hiding its shape. Here is an example of a boxed array:

┌─┬───────┬─────┐
│0│1 2 3 4│0 1 2│
│ │       │3 4 5│
└─┴───────┴─────┘

This array was generated from the J sentence 0 ; 1 2 3 4 ; i. 2 3 and contains three boxes, the first containing the scalar 0, the second containing a vector of shape 4, and the third containing a matrix of shape 2 3.

Some arrays of boxes look like they are plain arrays broken apart by boxes:

┌─┬───────────┐
│0│1 2 3 4    │
├─┼───────────┤
│5│ 9 10 11 12│
│6│13 14 15 16│
│7│17 18 19 20│
│8│21 22 23 24│
└─┴───────────┘

The previous array was generated from the J expression 2 2 $ 0 ; 1 2 3 4 ; (,. 5 6 7 8) ; 9 + i. 4 4.

Your goal in this challenge is to take an array like that and remove the boxes separating the subarrays from one another. For instance, the previous array would be transformed into

0  1  2  3  4
5  9 10 11 12
6 13 14 15 16
7 17 18 19 20
8 21 22 23 24

Submit a solution as a monadic verb / function. The verb must work on arrays of arbitrary rank. You may assume that the shapes of the boxed arrays fit, that is, boxed subarrays adjacent on an axis have to have the same dimensions in all but that axis. Behaviour is undefined if they don't.

This challenge is code golf. The submission compromising the least amount of characters wins.

\$\endgroup\$
  • \$\begingroup\$ Which languages are considered part of the APL family? \$\endgroup\$ – Alex A. Apr 1 '15 at 15:37
  • \$\begingroup\$ @AlexA. Languages like APL, J, and K. \$\endgroup\$ – FUZxxl Apr 1 '15 at 15:37
  • \$\begingroup\$ @randomra An array of boxes such that the arrays in the boxes if concatenated along the respective axes would form one large array. Imagine each boxed subarray to be a cuboid or rectangle and you put these together as specified by the boxes' positions to get a large cuboid or rectangle. \$\endgroup\$ – FUZxxl Apr 1 '15 at 15:46
  • \$\begingroup\$ @randomra Your input would be considered invalid as the subarrays are not directly concatenable along the axes they would be concatenated. \$\endgroup\$ – FUZxxl Apr 1 '15 at 15:51
  • 3
    \$\begingroup\$ In the interest of making this less language-specific, why not provide the example also using simple nested array notation for other languages? \$\endgroup\$ – Martin Ender Apr 1 '15 at 18:43
1
\$\begingroup\$

J, 73 chars

f=.3 :0
>".'z',~,(',"','&.>/',~":)"*1+i.#$z=.(,$~$,~1#~(#$y)-#@$)&.>y
)

Usage and tests:

   a=.2 2 $ 0 ; 1 2 3 4 ; (,. 5 6 7 8) ; 9 + i. 4 4

   f a
0  1  2  3  4
5  9 10 11 12
6 13 14 15 16
7 17 18 19 20
8 21 22 23 24

   f a,:a  NB. rank 3 test
0  1  2  3  4
5  9 10 11 12
6 13 14 15 16
7 17 18 19 20
8 21 22 23 24

0  1  2  3  4
5  9 10 11 12
6 13 14 15 16
7 17 18 19 20
8 21 22 23 24

Golfing and explanation coming tomorrow.

\$\endgroup\$
  • 2
    \$\begingroup\$ Your solutions looks like you took a sledgehammer to the problem and beat it with brute force until it ceased to exist. \$\endgroup\$ – FUZxxl Apr 1 '15 at 18:43
  • \$\begingroup\$ @FUZxxl Exactly. Interested in nice solutions. \$\endgroup\$ – randomra Apr 1 '15 at 18:47
1
\$\begingroup\$

J, 39

f=:([:;(([:(;"1@|:)(;/&> ::]))&.>)@<"1)

Usage and tests

a=.2 2 $ 0 ; 1 2 3 4 ; (,. 5 6 7 8) ; 9 + i. 4 4
   f a
0  1  2  3  4
5  9 10 11 12
6 13 14 15 16
7 17 18 19 20
8 21 22 23 24
\$\endgroup\$
  • \$\begingroup\$ Is the name for v really needed? This csn be shortened I think. \$\endgroup\$ – FUZxxl Apr 1 '15 at 20:33
  • \$\begingroup\$ good point. shortened \$\endgroup\$ – joebo Apr 1 '15 at 20:37
  • \$\begingroup\$ You can leave out some parentheses: [:;([:;"1@|:;/&> ::])&.>@<"1 it's not needed to assign the verb to a name either. \$\endgroup\$ – FUZxxl Apr 2 '15 at 10:45
  • \$\begingroup\$ This looks a little bit like it would only work with two-dimensional test-cases. \$\endgroup\$ – FUZxxl Apr 2 '15 at 10:46

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.