Task1
We check for primality of the binary prefixes:
#!/usr/bin/env perl
use strict;
use warnings;
use Math::Prime::Util qw(is_prime);
use Data::Show;
sub binary_prefix{
my ($arr) = @_;
my ($bin,@res) = ('');
map{$bin .= $_; push @res,is_prime(oct("0b$bin")) ? 1 : 0} @$arr;
@res
}
print show binary_prefix([1,0,1]);
print show binary_prefix([1,1,0]);
print show binary_prefix([1,1,1,1,0,1,0,0,0,0,1,0,1,0,0,1,0,0,0,1]);
Task2
We sort the array according to the alien dictionary:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
sub alien_dictionary{
my ($words,$alien) = @_;
my $r = join '',@$alien;
sort{eval "\$a =~ tr/a-z/$r/r" cmp
eval "\$b =~ tr/a-z/$r/r"} @$words
}
print show alien_dictionary(['perl','python','raku'],
[qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/]);
print show alien_dictionary(['the','weekly','challenge'],
[qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/]);
No comments:
Post a Comment