26
4
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|||||+-----+|| | ||
||||+-------+|||+-------+| | ||
|||+---------+|| | | ||
||+-----------+| | | ||
|+-------------+---------+-----+-+|
+---------------------------------+
If my language has no nested arrays, can I disregard the definition of the data type? – ThreeFx – 2016-10-22T19:40:39.403
1@ThreeFx You could also take the input as a string representing the nested array – miles – 2016-10-22T19:44:27.813
That is REALLY inefficient (in Haskell). I'd have to parse numbers manually and so on. In that case it would be shorter to define and use the datatype. – ThreeFx – 2016-10-22T19:45:33.427
@ThreeFx Or you could pad the array with sentinel values such as
-1
since I also limited the integers to be non-negative. Then would just have to clean the output for those invalid values. – miles – 2016-10-22T19:47:31.230Sorry about deleting the post I made, I need to fix it up because it wasn't working for one of the test cases. – Gabriel Benamy – 2016-10-22T22:45:49.157
So I guess J's builtin box type representation is disallowed? – Conor O'Brien – 2016-10-22T22:53:10.303
@ConorO'Brien Yes, assuming the default is ascii representation, it completely trivializes the challenge so I had to ban builtins. Also, the sample cases were created in J – miles – 2016-10-22T22:55:46.840
@miles yeah, I figured they were :) – Conor O'Brien – 2016-10-22T23:31:04.537
Extending the bottom border of each inner box downward as much as possible so that the boxes are symmetric across horizontal axis through middle is not valid, I assume? Or would you allow it? I can show you an example if you want, but not now because on phone. – Mitch Schwartz – 2016-10-31T22:16:37.767
Would input format like
(1 2 3)
instead of[1, 2, 3]
be acceptable? – Mitch Schwartz – 2016-10-31T23:49:50.077Example of extending the boxes downward: http://chat.stackexchange.com/transcript/message/33239514#33239514
– Mitch Schwartz – 2016-11-01T00:46:41.9601@MitchSchwartz Sure, take the input in a nested tuple or whatever format is native to your language. Your output is fine as long as you remain consistent. Integers can be drawn at the top, center, or bottom, and the boxes can be at the top, center, bottom, or stretched to fill out their space like in your example. – miles – 2016-11-01T02:54:50.727
no test cases with multi-digit integers? – Sparr – 2016-11-01T10:46:30.290
May I have a checkmark? – Adám – 2016-11-01T23:59:54.967
@Adám If you have time, could you add an explanation to your answer. – miles – 2016-11-03T00:48:28.370
@miles Good enough? – Adám – 2016-11-03T08:58:33.533
@Adám That's great, thanks – miles – 2016-11-03T09:02:23.893