Task1
We can find the largest number that can be formed from an array of numbers by sorting the string representation of those numbers:
#!/usr/bin/env perl
use strict;
use warnings;
sub largest_number{
join '',sort {$b.$a <=> $a.$b} @{$_[0]}
}
printf "%s\n",largest_number([20,3]);
printf "%s\n",largest_number([3,30,34,5,9]);
Task2
We find the hamming distance of all the combinations of numbers and sum them:
#!/usr/bin/env perl
use strict;
use warnings;
use Algorithm::Combinatorics qw(combinations);
use List::Util qw(sum0);
sub hamming_distance{
sum0 map {sum0 split '',sprintf "%b",$_->[0] ^ $_->[1]}
combinations($_[0],2)
}
printf "%d\n",hamming_distance([4,14,2]);
printf "%d\n",hamming_distance([4,14,4]);
No comments:
Post a Comment