Task1
We check if name is a subset of typed:
#!/usr/bin/env perl
use strict;
use warnings;
use Set::Scalar;
sub broken_keys{
my $s1 = Set::Scalar->new(split '',$_[0]);
my $s2 = Set::Scalar->new(split '',$_[1]);
$s1 <= $s2
}
printf "%d\n",broken_keys('perl','perrrl');
printf "%d\n",broken_keys('raku','rrakuuuu');
printf "%d\n",broken_keys('python','perl');
printf "%d\n",broken_keys('coffeescript','cofffeescccript');
Task2
We make an array from the input string and only reverse the letters:
#!/usr/bin/env perl
use strict;
use warnings;
sub reverse_letters{
my @chars = split '',$_[0];
my ($i,$j) = (0,$#chars);
while($i < $j){
$i++ while($i < $j and $chars[$i] !~ /[[:alpha:]]/);
$j-- while($i < $j and $chars[$j] !~ /[[:alpha:]]/);
($chars[$i++],$chars[$j--]) = ($chars[$j],$chars[$i]) if $i < $j;
}
join '',@chars
}
printf "%s\n",reverse_letters('p-er?l');
printf "%s\n",reverse_letters('wee-k!L-y');
printf "%s\n",reverse_letters('_c-!h_all-en!g_e');
No comments:
Post a Comment