Given a possibly nested, non-empty array of single-digit positive integers (not guaranteed unique), output the ASCII-art representation as a tree, using the box-drawing characters ┌ ┴ ┐ ─ │ ┬ ┼
. (These were copied from Code Page 437, but you can use any equivalent representation).
Every integer of the array should be a leaf of the tree. Elements the same level deep in the array should be present at the same level of the tree. All elements should be separated by enough whitespace to be distinct (up to you to determine how wide, minimum of one space between).
For example, given array [[1, [2]], [3, [4, 5]]]
, output the following tree
┌─┴─┐
┌┴┐ ┌┴─┐
1 │ 3 ┌┴┐
2 4 5
For array [1, 2, 3]
the tree could look like
┌─┼─┐
1 2 3
But the array [[1, 2, 3]]
would look like
│
┌─┼─┐
1 2 3
While the array [1, [1, [1, [1]]]]
could look like
┌─┴┐
1 ┌┴─┐
1 ┌┴┐
1 │
1
As a more complicated example, [1, [[[2, 3], 4], 5]]
could be
┌┴───┐
1 ┌─┴┐
┌─┴┐ 5
┌┴┐ 4
2 3
or several other variations.
- Input and output can be given by any convenient method.
- You can print it to STDOUT or return it as a function result.
- Either a full program or a function are acceptable.
- Any amount of extraneous whitespace is acceptable, so long as the characters line up appropriately.
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
[1,[[[2,3],4],5]]
could be an interesting test case since it needs to have the root artificially extend so the right subtree doesn't collide with the left subtree. \$\endgroup\$1
is an array of 3 items:[2,3]
,4
, and5
. But 4 and 5 are not adjacent. \$\endgroup\$[1, [[[2, 3]], [4], 5]]
to me. \$\endgroup\$