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\$
17
  • \$\begingroup\$ Which languages are considered part of the APL family? \$\endgroup\$
    – Alex A.
    Commented Apr 1, 2015 at 15:37
  • \$\begingroup\$ @AlexA. Languages like APL, J, and K. \$\endgroup\$
    – FUZxxl
    Commented Apr 1, 2015 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
    Commented Apr 1, 2015 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
    Commented Apr 1, 2015 at 15:51
  • 4
    \$\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\$ Commented Apr 1, 2015 at 18:43

2 Answers 2

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
  • 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
    Commented Apr 1, 2015 at 18:43
  • \$\begingroup\$ @FUZxxl Exactly. Interested in nice solutions. \$\endgroup\$
    – randomra
    Commented Apr 1, 2015 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\$
4
  • \$\begingroup\$ Is the name for v really needed? This csn be shortened I think. \$\endgroup\$
    – FUZxxl
    Commented Apr 1, 2015 at 20:33
  • \$\begingroup\$ good point. shortened \$\endgroup\$
    – joebo
    Commented Apr 1, 2015 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
    Commented Apr 2, 2015 at 10:45
  • \$\begingroup\$ This looks a little bit like it would only work with two-dimensional test-cases. \$\endgroup\$
    – FUZxxl
    Commented Apr 2, 2015 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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