Task1
We apply split on each element of the array then filter those which are of length > 0:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
sub split_strings{
my ($arr,$sep) = @_;
grep {length} map{split /\Q$sep\E/} @$arr
}
print show split_strings(['one.two.three','four.five','six'],'.');
print show split_strings(['$perl$$', '$$raku$'],'$');
Task2
We sort the indices according to the sum of each subarray in the matrix, or in case of them being equal, on the indices themselves:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
use List::Util qw(sum0);
sub weakest_row{
my ($mat) = @_;
sort{sum0(@{$$mat[$a]}) <=> sum0(@{$$mat[$b]}) || $a <=> $b}
0..$#$mat
}
print show weakest_row([[1,1,0,0,0],
[1,1,1,1,0],
[1,0,0,0,0],
[1,1,0,0,0],
[1,1,1,1,1]]);
print show weakest_row([[1,0,0,0],
[1,1,1,1],
[1,0,0,0],
[1,0,0,0]]);
No comments:
Post a Comment