diff options
| -rw-r--r-- | challenge-211/carlos-oliveira/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-211/carlos-oliveira/perl/ch-2.pl | 19 |
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-211/carlos-oliveira/perl/ch-1.pl b/challenge-211/carlos-oliveira/perl/ch-1.pl new file mode 100644 index 0000000000..bfd6351111 --- /dev/null +++ b/challenge-211/carlos-oliveira/perl/ch-1.pl @@ -0,0 +1,27 @@ +use strict; +use warnings; +use v5.36; + +use Test::More; +use List::Util qw(uniq); + +sub is_toeplitz_matrix (@matrix) { + my $cols = $matrix[0]->@*; + for my $i (0..$#matrix) { + my @diagonal = uniq map { $i < $cols ? $_->[$i++] : () } @matrix; + return 0 unless @diagonal == 1; + } + return 1; +} + +is is_toeplitz_matrix( + [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3] +), 1; +is is_toeplitz_matrix( + [1, 2, 3], + [3, 2, 1] +), 0; + +done_testing; diff --git a/challenge-211/carlos-oliveira/perl/ch-2.pl b/challenge-211/carlos-oliveira/perl/ch-2.pl new file mode 100644 index 0000000000..421b8e94d7 --- /dev/null +++ b/challenge-211/carlos-oliveira/perl/ch-2.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use v5.36; + +use Test::More; +use Algorithm::Combinatorics qw(partitions); +use List::Util qw(any sum uniqnum); +use builtin qw(true false); + +sub can_be_split_with_same_average (@array) { + return any { $_->@* == 1 } + map { [ uniqnum map sum($_->@*) / $_->@*, $_->@* ] } + partitions \@array, 2; +} + +is can_be_split_with_same_average(1, 2, 3, 4, 5, 6, 7, 8), true; +is can_be_split_with_same_average(1, 3), false; + +done_testing; |
