Given an array of any depth, draw its contents with borders of +-|
around each subarray. Those are the ASCII characters for plus, minus, and vertical pipe.
For example, if the array is [1, 2, 3]
, draw
+-----+
|1 2 3|
+-----+
For a nested array such as [[1, 2, 3], [4, 5], [6, 7, 8]]
, draw
+-----------------+
|+-----+---+-----+|
||1 2 3|4 5|6 7 8||
|+-----+---+-----+|
+-----------------+
For a ragged array such as [[[1, 2, 3], [4, 5]], [6, 7, 8]]
, draw
+-------------------+
|+-----------+-----+|
||+-----+---+|6 7 8||
|||1 2 3|4 5|| ||
||+-----+---+| ||
|+-----------+-----+|
+-------------------+
Notice that there is more space after drawing [6, 7, 8]
. You may either draw the contents on the top-most, center, or bottom-most line, but whichever you choose, you must remain consistent.
This challenge was inspired by the box verb <
from J.
Rules
- This is code-golf so the shortest code wins.
- Builtins that solve this are not allowed.
- The input array will contain only nonnegative integer values or arrays. Each array will be homogenous, meaning that its elements will either by only arrays or only integers, but never a mix of both.
- Each subarray may be nested to any depth.
- The output may either by as a string or as an array of strings where each string is a line of output.
Test Cases
[]
++
||
++
[[], []]
+---+
|+++|
|||||
|+++|
+---+
[[], [1], [], [2], [], [3], []]
+-----------+
|++-++-++-++|
|||1||2||3|||
|++-++-++-++|
+-----------+
[[[[[0]]]]]
+---------+
|+-------+|
||+-----+||
|||+---+|||
||||+-+||||
|||||0|||||
||||+-+||||
|||+---+|||
||+-----+||
|+-------+|
+---------+
[[[[[4, 3, 2, 1]]]], [[[3, 2, 1]]], [[2, 1]], [1]]
+---------------------------------+
|+-------------+---------+-----+-+|
||+-----------+|+-------+|+---+|1||
|||+---------+|||+-----+|||2 1|| ||
||||+-------+|||||3 2 1|||+---+| ||
|||||4 3 2 1|||||+-----+|| | ||
||||+-------+|||+-------+| | ||
|||+---------+|| | | ||
||+-----------+| | | ||
|+-------------+---------+-----+-+|
+---------------------------------+
-1
since I also limited the integers to be non-negative. Then would just have to clean the output for those invalid values. \$\endgroup\$