24
4
Inspired by A014486.
Challenge
Given an integer input in base 10, construct a representation for the binary forest corresponding to the input. Representations include, but are not limited to, nested arrays and strings.
How?
Convert the input to binary. 1
s represent branches, and 0
s represent leaves.
To make this easier to understand, let's use 834
(1101000010 in binary) as an example.
We start with the first digit. The first digit is a 1
, so we draw branches:
\ / 1
or as an array, {{1}}
The next digit is 1
, so we draw more branches (we go from left to right):
\ / 1 \ / 1
or as an array, {{1, {1}}}
The next digit is 0
, so we place a leaf:
0 \ / 1 \ / 1
or as an array, {{1, {1, 0}}}
The next digit is a 1
, so we place a branch:
\ / 0 1 \ / 1 \ / 1
or as an array, {{1, {1, 0, {1}}}}
Repeating the process, we obtain the following tree after the 8th digit:
0 0 \ / 0 1 \ / 1 0 \ / 1
or as an array, {{1, {1, 0, {1, 0, 0}}, 0}}
For the remaining digits, we draw more trees:
The 9th digit is a 0
, so we place a leaf (aww, it's a young shoot!)
0 0 \ / 0 1 \ / 1 0 \ / 1 0
or as an array, {{1, {1, 0, {1, 0, 0}}, 0}, 0}
When we use all the digits, we end up with this:
0 0 \ / 0 1 \ / 1 0 0 \ / \ / 1 0 1
or as an array, {{1, {1, 0, {1, 0, 0}}, 0}, 0, {1, 0}}
That looks weird, so we pad a zero to complete the tree:
0 0 \ / 0 1 \ / 1 0 0 0 \ / \ / 1 0 1
or as an array, {{1, {1, 0, {1, 0, 0}}, 0}, 0, {1, 0, 0}}
Note that the flattening the array yields the original number in binary, but with a padded zero.
Criteria
- The output must clearly show the separation of the trees and branches (if it is not a nested array, please explain your output format).
- Extracting all digits from the output must be identical to the binary representation of the input (with the padded zero(s) from the above process).
Test cases
The output may differ as long as it meets the criteria.
0 -> {0} 1 -> {{1, 0, 0}} 44 -> {{1, 0, {1, {1, 0, 0}, 0}}} 63 -> {{1, {1, {1, {1, {1, {1, 0, 0}, 0}, 0}, 0}, 0}, 0}} 404 -> {{1, {1, 0, 0}, {1, 0, {1, 0, 0}}}} 1337 -> {{1, 0, {1, 0, 0}}, {1, {1, {1, 0, 0}, {1, 0, 0}}, 0}}
Scoring
This is code-golf, so lowest bytes wins!
5
I would avoid using bonuses - it generally doesn't improve the challenge.
– Sanchises – 2016-12-18T09:05:16.0871@Sanchises I added the bonus to see answers with visualization... How else could I encourage people to try making a visualization as the output? – JungHwan Min – 2016-12-18T15:18:28.673
4(re your comment) Require it? – msh210 – 2016-12-18T17:51:17.500
1
@JungHwanMin Look at what I linked in more detail (especially the comments); or, in the same Meta question, this answer. Your current question asks for posters to create 2^2=4 programs, and calculate the score of each program, before submitting the best scoring program. Either require it when you think it makes for a better challenge, or remove it if you feel it makes for a worse challenge.
– Sanchises – 2016-12-18T19:09:22.3772@JungHwanMin Fair enough. They must golf 3 programs and calculate each individual score before submitting an answer. What you have here, is three challenges wrapped into one. I would recommend posting the visualization as a separate challenge. – Sanchises – 2016-12-18T19:30:09.627
Is this possible in Homespring? If so somebody do it (homespring is related because the trees are constructed somewhat similar to this) – Destructible Lemon – 2016-12-20T22:06:49.890
How large numbers must we support? Is 9*10^15 (53 bits) a large enough upper limit? – PurkkaKoodari – 2016-12-26T00:53:29.013
@Pietu1998 Yep. That's good enough. – JungHwan Min – 2016-12-27T03:46:48.313
is visualisation still allowed? It does seem to be. – Destructible Lemon – 2016-12-27T05:02:34.840
@DestructibleWatermelon of course, as long as it represents the binary forest – JungHwan Min – 2016-12-27T05:34:24.357
that seems slightly unclear. doesn't a number then represent the binary forest? – Destructible Lemon – 2016-12-27T05:56:09.870
@DestructibleWatermelon I want the output to have clear separation of branches and trees – JungHwan Min – 2016-12-27T06:06:10.827