Task1
We check if every row and column contains digits from 1 to n:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
sub same_row_column{
my ($m) = @_;
my $n = @$m;
foreach my $row (@$m){
my %h;
foreach my $num(@$row){
return 0 if ($num < 1 || $num > $n || $h{$num}++);
}
}
foreach my $col(0..$n-1){
my %h;
foreach my $row(@$m){
my $num = $row->[$col];
return 0 if ($num < 1 || $num > $n || $h{$num}++);
}
}
1
}
is same_row_column([[1,2,3,4],[2,3,4,1],
[3,4,1,2],[4,1,2,3]]),1,'Example 1';
is same_row_column([[1]]),1,'Example 2';
is same_row_column([[1,2,5],[5,1,2],[2,5,1]]),0,'Example 3';
is same_row_column([[1,2,3],[1,2,3],[1,2,3]]),0,'Example 4';
is same_row_column([[1,2,3],[3,2,1],[3,2,1]]),0,'Example 5';
done_testing();
Task2
We count the numbers which have strictly smaller and greater elements than themselves:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
sub smaller_greater_element{
my ($arr) = @_;
my ($min,$max) = ($arr->[0],$arr->[0]);
($_ < $min) ? ($min = $_)
: ($max < $_) && ($max = $_) foreach @$arr;
@$arr - grep {$min == $_ || $max == $_} @$arr
}
is smaller_greater_element([2,4]),0,'Example 1';
is smaller_greater_element([1,1,1,1]),0,'Example 2';
is smaller_greater_element([1,1,4,8,12,12]),2,'Example 3';
is smaller_greater_element([3,6,6,9]),2,'Example 4';
is smaller_greater_element([0,-5,10,-2,4]),3,'Example 5';
done_testing();