24
2
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. – Poke – 2019-02-13T20:39:35.363@Poke Added as an example. There are several possible variations for that test case. – AdmBorkBork – 2019-02-13T21:09:21.393
2The first example for that test case can't be right. That suggests that the s second element next to the
1
is an array of 3 items:[2,3]
,4
, and5
. But 4 and 5 are not adjacent. – Draco18s no longer trusts SE – 2019-02-13T21:34:18.3874That looks like
[1, [[[2, 3]], [4], 5]]
to me. – Neil – 2019-02-13T21:45:28.157Which (if any) of these alternative input formats would be acceptable?
– Οurous – 2019-02-13T22:02:55.473@neil I could agree with that, too. Didn't think about a one element array. – Draco18s no longer trusts SE – 2019-02-13T22:03:52.463
How should I chose from these outputs. Or are they both OK?
– tsh – 2019-02-14T03:05:44.980@Neil Yep - you're right. I wasn't thinking clearly when I made that example. – AdmBorkBork – 2019-02-14T13:37:00.173
@Οurous Any of those inputs are fine. – AdmBorkBork – 2019-02-14T13:37:28.637
@tsh Either of those outputs are OK. – AdmBorkBork – 2019-02-14T13:37:43.557
What are we allowed to do / supposed to do to prevent the elements from colliding? – Post Rock Garf Hunter – 2019-02-21T03:38:15.653
@SriotchilismO'Zaic Any amount of whitespace you want, minimum of one space between elements. – AdmBorkBork – 2019-02-21T14:51:09.583
For your last example would it be ok if the
2
were not under the1
and rather to the left or the right for example? – Post Rock Garf Hunter – 2019-02-21T14:52:07.787@SriotchilismO'Zaic Yes, that would be fine. The only requirement is that the elements are made distinct from each other. – AdmBorkBork – 2019-02-21T14:53:02.233