blob: 170d3c7e9b26f3a4385e193d4f2dd5bfd0ec262e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use experimental 'signatures';
sub one_bits($int) {
# Convert the integer to binary, and count the number of 1s
my $bin = sprintf( "%b", $int );
return ($bin =~ tr/1/1/);
}
sub sort_by_1_bits (@ints) {
# Sort the integers by the number of 1 bits, and by the integer value if
# the number of 1 bits is the same
my @sorted = sort { one_bits($a) <=> one_bits($b) || $a <=> $b } @ints;
say '(' . join( ', ', @sorted ) . ')';
}
sort_by_1_bits(@ARGV);
|