Task1
We count the words that doesn't include given chars:
#!/usr/bin/env perl
use strict;
use warnings;
sub broken_keyboard{
my $chars = join '',@{$_[1]};
scalar grep {$chars eq '' || !/[$chars]/i} split /\W+/,$_[0];
}
printf "%d\n",broken_keyboard('Hello World',['d']);
printf "%d\n",broken_keyboard('apple banana cherry',['a','e']);
printf "%d\n",broken_keyboard('Coding is fun',['']);
printf "%d\n",broken_keyboard('The Weekly Challenge',['a','b']);
printf "%d\n",broken_keyboard('Perl and Python',['p'])
Task2
We reverse the prefix before and including the given char and append to the rest of the string:
#!/usr/bin/env perl
use strict;
use warnings;
sub reverse_prefix{
my ($str) = @_;
$str =~ s/^(.*?$_[1])/reverse $1/e;
$str
}
printf "%s\n",reverse_prefix('programming','g');
printf "%s\n",reverse_prefix('hello','h');
printf "%s\n",reverse_prefix('abcdefghij','h');
printf "%s\n",reverse_prefix('reverse','s');
printf "%s\n",reverse_prefix('perl','r')
No comments:
Post a Comment