Task1
We iterate the array, and if the result of index mod 10 is equal to the array's element at that index, we return that index, otherwise we return -1:
#!/usr/bin/env perl
use strict;
use warnings;
sub smallest_index{
map{return $_ if $_ % 10 == $_[0]->[$_]}0..$#{$_[0]};
-1
}
printf "%d\n",smallest_index([0,1,2]);
printf "%d\n",smallest_index([4,3,2,1]);
printf "%d\n",smallest_index([1,2,3,4,5,6,7,8,9,0]);
Task2
We iterate the array, if the element is numeric, we use it, else we get the length of the string and assign it to $n, then get the max of array:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);
sub alphanumeric_string_value{
max map{my $n = (/^\d+$/) ? $_ : length}@{$_[0]};
}
printf "%d\n",alphanumeric_string_value(['perl','2','000','python','r4ku']);
printf "%d\n",alphanumeric_string_value(['001','1','000','0001']);
No comments:
Post a Comment