Task1
We check if there are 3 consecutive identical digits:
#!/usr/bin/env perl
use strict;
use warnings;
sub good_integer {
$_[0] =~ /(\d)\1{2}(\1)?/ ? ($2 ? -1 : $1 x 3) : ()
}
print good_integer(12344456),"\n";
print good_integer(1233334),"\n";
print good_integer(10020003),"\n";
Task2
We count the number of letter changes:
#!/usr/bin/env perl
use strict;
use warnings;
sub changing_keys{
my ($c,@arr) = (0,split '',lc $_[0]);
foreach my $i(0..$#arr-1){
$c++ if $arr[$i] ne $arr[$i+1]
}
$c
}
printf "%d\n",changing_keys('pPeERrLl');
printf "%d\n",changing_keys('rRr');
printf "%d\n",changing_keys('GoO');
No comments:
Post a Comment