18
... Ah sorry, no popcorn here, just POPCNT.
Write the shortest program or function which takes in a number n
and output all integers from 0 to 2n - 1, in ascending order of number of 1 bits in the binary representation of the numbers (popcount). No duplicates allowed.
The order of numbers with the same popcount is implementation-defined.
For example, for n = 3
, all these output are valid:
0, 1, 2, 4, 3, 5, 6, 7
[0, 4, 1, 2, 5, 3, 6, 7]
0 4 2 1 6 5 3 7
The input and output format are implementation-defined to allow the use of language features to further golf the code. There are a few restrictions on the output:
- The numbers must be output in decimal format.
The output must contain a reasonable separator between the numbers (trailing separator allowed, but not leading).
Line feed (
\n
), tab (\t
), space,,
,.
,;
,|
,-
,_
,/
are quite reasonable separator. I don't mind additional spaces for pretty printing, but don't use letter or digits as separators.- The numbers and separators can be surrounded by
[ ]
,{ }
or any array or list notation. - Don't print anything else not stated above.
Bonus
Multiply your score by 0.5 if your solution can generate the number on the fly. The spirit of this bonus is that if you were to directly convert your printing solution to a generator, the generator only uses at most O(n) memory where n is the number of bits as defined above. (You don't have to actually convert your solution to generator). Note that while I impose n <= 28, the memory needed to store all numbers still grows exponentially, and a naive sorting solution would hog up at least 4 GB of memory at n = 28.
Please add some simple explanation of how your solution works before claiming this bonus.
4It seems that the challenge as it is quite boring and would result in a bunch of sorting answers. I would like to add some bonus to make the challenge more interesting. Something along the line of "generating the numbers on the fly". If you are OK with it, please upvote this comment, then I will add it to the question. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2015-02-27T05:29:46.780
If you disagree, please upvote this comment. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2015-02-27T05:30:32.543
Please use the sandbox to ask for further suggestions on a question before posting it live. – John Dvorak – 2015-02-27T07:30:09.233
21@JanDvorak: It was on sandbox for one month. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2015-02-27T07:59:43.040
I believe your incentive for finding a solution without sorting isn't enough. While I can think about some ways to compute the table without sorting, their implementation would be more than twice as long as my current code. – FUZxxl – 2015-02-28T02:37:28.073
@FUZxxl: Agree, since no answer is going to get shorter than 9 characters. However, it's quite hard to change the requirement at this point. If you have any idea on how to improve the question, please let me know. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2015-02-28T02:45:46.187
1I think it's too late for this question. Generally, questions where you have to figure out a non-trivial algorithm aren't well suited for code golf in my opinion. Make them a code challenge instead and pose all the constraints you need. – FUZxxl – 2015-02-28T02:48:31.263
@FUZxxl: I will keep that in mind next time. Actually PhiNotPi also suggested code challenge, but I forgot the initial goal of this question (as I found the solution previously), so I post the challenge as a code golf regardless. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2015-02-28T02:55:20.407