| Bytes | Lang | Time | Link |
|---|---|---|---|
| 005 | Vyxal | 240726T015748Z | emanresu |
| 012 | Uiua | 240723T201459Z | nyxbird |
| 009 | JCram | 240723T132808Z | Kamila S |
| 014 | Jelly | 170803T175534Z | Erik the |
| 065 | Bash | 161127T101901Z | zeppelin |
| 068 | MATLAB | 161126T222938Z | PieCot |
| 049 | PHP | 161122T051144Z | Titus |
| 129 | Ruby 2.x | 161122T032533Z | freakyfe |
| 063 | Perl | 161105T223902Z | Dada |
| 109 | Racket | 161106T044903Z | rnso |
| 098 | Python 2 | 161103T032752Z | Wheat Wi |
| 102 | Perl | 161105T024236Z | Gabriel |
| 146 | Python 2 | 161105T040541Z | Daniel |
| 078 | Haskell | 161104T013758Z | nimi |
Uiua, 12 bytes
⊏⍏≡/+.⇌⋯⇡ⁿ:2
⊏⍏≡/+.⇌⋯⇡ⁿ:2
⇡ⁿ:2 # range from 0 to 2^n-1,
⋯ # convert to binary arrays,
⇌ # reverse,
⊏ . # sort by
⍏ # ascending
≡/+ # bitsums
JCram, 9 bytes (SBCS)
∩T⍺⁹H↓Ͼ
Encodes the following ES6 program:
n=>[...Array(1<<n)].map((_,i)=>i.toString(2).padStart(n,0))
Bash, 65 bytes
Golfed
seq -f "obase=2;%g+2^$1-1" $[2**$1]|bc|cut -c2-|tr 0 ~|sort|tr ~ 0
Test
>./binseq 4
0000
0001
0010
0100
1000
0011
0101
0110
1001
1010
1100
0111
1011
1101
1110
1111
MATLAB, 68 bytes
d=@(n)dec2bin(sortrows([sum(dec2bin(0:2^n-1)');0:2^n-1]')*~eye(2,1))
PHP, 49 bytes
while($i<1<<$n=$argv[1])printf("%0${n}b\n",$i++);
Run with -r.
Ruby 2.x, 129 bytes
f=->(n){z='0';p=n.bit_length;(0..n).map{|i|sprintf("%0#{p}d",i.to_s(2))}.sort{|a,b|a=a.delete(z).size-(b=b.delete(z).size)||b-a}}
Perl, 63 bytes
-4 thanks to @Ton Hospel.
-2 thanks to @Gabriel Benamy.
say~~reverse for sort{$b=~y/0//-$a=~y/0//||$b-$a}glob"{1,0}"x<>
Run with -E (which enable the feature say) :
perl -E 'say~~reverse for sort{$b=~y/0//-$a=~y/0//||$b-$a}glob"{1,0}"x<>' <<< 5
Short explanations:
"{1,0}"x$_creates a string composed of$_times{1,0}($_is the input). For instance with3:{1,1}{1,0}{1,0}.- Then
globdoes some magic and generates all combinations of one element from each group of braces (that is, all the combinations we want to print). - And then the sort :
$b=~y/1//c-$a=~y/1//ccompares the number of1in each string, and if they have the same number,$b-$awill sort according to the second rule.
Racket 109 bytes
(let loop((s ""))(if(= n(string-length s))(displayln s)(for((i '("0" "1")))(loop(string-append s i)))))
Ungolfed:
(define (f n)
(let loop ((s ""))
(if (= n (string-length s))
(displayln s)
(for ((i '("0" "1")))
(loop (string-append s i))))))
Testing:
(f 2)
(println "-------------")
(f 3)
(println "-------------")
(f 4)
Output:
00
01
10
11
"-------------"
000
001
010
011
100
101
110
111
"-------------"
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Python 2, 122 120 102 98 bytes
18 bytes saved thanks to Flp.Tkc
4 bytes saved thanks to xnor
lambda x:sorted([bin(i)[2:].zfill(x)for i in range(2**x)],key=lambda x:(sorted(x),int(x[::-1],2)))
Explanation
This makes all the binary strings of length x with:
[bin(i)[2:].xfill(x)for i in range(2**x)]
I then sort them according to:
lambda x:(sorted(x),int(x[::-1],2))
sorted(x) prioritizes the number of 1s while int(x[::-1],2) prioritizes the second condition
Lastly these are joined with newlines and printed.
Perl, 116 106 105 102 bytes
sub e{sprintf"%0$%b",@_}sub f{$_=e@_;/0*$/;$%*y/1//-$-[0]}map{say e$_}sort{f($a)<=>f$b}0..2**($%=<>)-1
Readable:
sub e{
sprintf"%0$%b",@_
}
sub f{
$_=e@_;/0*$/;$%*y/1//-$-[0]
}
map{
say e$_
} sort {
f($a)<=>f$b
} 0..2**($%=<>)-1
The subroutine e converts its argument into a binary value, padded with zeros, to be the input length (e.g. input of 5 pads with zeros until it's 5 characters long). The subroutine f takes such a binary value, and gives it sorting weight according to how it should be processed.
The range 0 .. [input]2-1 is then put through a stable sort, ordering by the weight (here, "stable" means that when two values have the same weight, they are returned in the same order they appear in the input), and then they are fed back to the subroutine e and output.
Some of you may have seen my original post, but I entirely misread the problem yesterday and deleted it immediately after I realized it.
Python 2, 146 bytes
from itertools import*
lambda i:sum([sorted({''.join(b)for b in permutations((i-n)*"0"+"1"*n)},key=lambda x:x[::-1])[::-1]for n in range(i+1)],[])
I'm still working on this, though any suggestions would be greatly appreciated!
Ungolfed
i=input()
import itertools
p=[]
for n in range(i+1):
x=(i-n)*"0"+"1"*n
t=[]
for b in itertools.permutations(x):t+=[''.join(b)] if ''.join(b) not in t else []
p.append(sorted(t, key=lambda x:x[::-1])[::-1])
p=sum(p,[])
print
for line in p:print line
Haskell, 78 bytes
import Data.List
f n=sortOn(\x->sum x:reverse(map(1-)x))$mapM id$[0,1]<$[1..n]
Usage example: f 2 -> [[0,0],[0,1],[1,0],[1,1]].
How it works:
[0,1]<$[1..n] -- make n copies of the list [0,1]
mapM id -- make all lists where the ith element is from the ith list.
-- that gives us all binary sequences
sortOn -- sort this list of list
sum x -- first by number of ones
reverse(map(1-)x) -- then by the reversed list with bits flipped