Task1
We repeatedly remove adjacent duplicate characters until there's none left:
#!/usr/bin/env perl
use strict;
use warnings;
sub duplicate_removals{
my ($str) = @_;
while(1) {
my $c = 0;
$str =~ s/(.)\1//g and $c = 1;
last unless $c
}
$str
}
printf "%s\n",duplicate_removals('abbaca');
printf "%s\n",duplicate_removals('azxxzy');
printf "%s\n",duplicate_removals('aaaaaaaa');
printf "%s\n",duplicate_removals('aabccba');
printf "%s\n",duplicate_removals('abcddcba')
Task2
We extract the numbers into an array, and check if that array is ascending:
#!/usr/bin/env perl
use strict;
use warnings;
sub is_ascending{
foreach my $i(1..$#{$_[0]}) {
return 0 if $_[0]->[$i] <= $_[0]->[$i-1]
}
1
}
sub ascending_numbers{
my @arr = grep {/\d+/} split /\s+/,$_[0];
is_ascending(\@arr)
}
printf "%d\n",ascending_numbers('The cat has 3 kittens 7 toys 10 beds');
printf "%d\n",ascending_numbers('Alice bought 5 apples 2 oranges 9 bananas');
printf "%d\n",ascending_numbers('I ran 1 mile 2 days 3 weeks 4 months');
printf "%d\n",ascending_numbers('Bob has 10 cars 10 bikes');
printf "%d\n",ascending_numbers('Zero is 0 one is 1 two is 2');
No comments:
Post a Comment