30
1
The challenge is simple: write a program or function that, when given a finite non-negative integer, outputs a nested array.
The rules
- Your code must produce a unique valid nested array for every integer 0 ≤ n < 231.
- Every possible nested array with up to 16 open brackets must be outputted within this range. (This does not mean that your code can never output a nested array with more than 16 open brackets.)
- Your code may output a string representation of the nested array instead of an actual array (with or without commas).
One possible mapping:
0 -> []
1 -> [[]]
2 -> [[[]]]
3 -> [[], []]
4 -> [[[[]]]]
5 -> [[[], []]]
6 -> [[[]], []]
7 -> [[], [[]]]
8 -> [[], [], []]
9 -> [[[[[]]]]]
etc.
Scoring
This is code-golf, so the shortest code in bytes wins.
Are there any time/memory restrictions? – Dennis – 2016-09-26T04:06:17.703
@Dennis Does 1 hour seem reasonable for a time restriction? I have no clue what's reasonable for memory. – ETHproductions – 2016-09-26T04:19:23.940
Memory isn't a big deal if there's a time limit. One hour seems very generous. I wouldn't want to wait a full hour to verify if my code is fast enough. – Dennis – 2016-09-26T04:47:46.613
@Dennis How about 1 minute, then? 5 minutes? 10? – ETHproductions – 2016-09-26T04:52:37.460
5 minutes seems a good spot between slow languages can't compete and I have to wait forever to test if my code is fast enough. Do you have a reference implementation? The naïve approach takes one minute for input 3000 in Jelly. – Dennis – 2016-09-26T04:58:59.163
4I'd prefer no time restrictions. That gives more scope for originality – Ton Hospel – 2016-09-26T06:23:47.800
Perl likes strings as input and output. So can I output the list members as a single string, e.g.
[],[]
or even without separator[][]
– Ton Hospel – 2016-09-26T06:26:06.257Related: http://codegolf.stackexchange.com/questions/66127/catalan-numbers
– alephalpha – 2016-09-26T11:15:26.467I'm not sure I understand. are there other valid outputs? could it go 2:
[[]]
, 3:[[[]]]
, 4:[[[[]]]]
, 5[[[[[]]]]]
, etc? – None – 2016-09-26T14:41:42.3302@TonHospel You may output without commas. I guess no time restrictions would be fine, as long as you can prove that your entry is valid. – ETHproductions – 2016-09-26T14:44:40.240
@tuskiomi As long as every possible array with up to 16 open-brackets is outputted within 0 ≤ n < 2^31, and the output for every integer within this range is unique and a valid array, the program is valid. I don't believe your example is valid because (at least from what I can see) it will never output
[[][]]
or anything like that. See the existing answers for examples of other valid outputs. – ETHproductions – 2016-09-26T14:51:33.797Am I right in saying that there are
2^14
possible lists with16
opening brackets? – Myridium – 2016-09-27T01:58:24.920@Myridium The number of possible lists with n opening brackets is the n-1'th Catalan number. There are 9694845 possible lists with 16 opening brackets.
– ETHproductions – 2016-09-27T02:03:34.747