From 6e98239f9d7df539606d6f6412d9aae8002851ae Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 3 Apr 2023 23:06:58 +0200 Subject: Solution to task 1 --- challenge-211/jo-37/perl/ch-1.pl | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 challenge-211/jo-37/perl/ch-1.pl diff --git a/challenge-211/jo-37/perl/ch-1.pl b/challenge-211/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..586b91905e --- /dev/null +++ b/challenge-211/jo-37/perl/ch-1.pl @@ -0,0 +1,85 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0 '!float'; +use PDL; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die < 1) has N + M - 3 diagonals having more than one +# element. Creating a set of NxN matrices where each of them have one +# of the orignal matrix' diagonals as its main diagonal. Then take the +# diagonal of these matrices and re-arrange them into a new matrix +# having the main diagonals of the matrix series as rows. Taking minimum +# and maximum over the rows. If min and max equals for every row, the +# matrix is Toeplitz. +# Note: Utilizing BAD values in incomplete diagonals that do not account +# for minimum or maximum. +sub is_toeplitz { + (my $m = pdl @_)->badflag(1); + my ($min, $max) = ( + cat map $_->diagonal(0, 1), + $m->range($m->dim(0) - 2 - sequence(indx, 1, $m->shape->sum - 3), + $m->dim(1), 't') + ->reorder(1,2,0)->dog + )->minmaximum; + + all $min == $max; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + ok is_toeplitz( + [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3]), 'example 1'; + + ok !is_toeplitz( + [1, 2, 3], + [3, 2, 1]), 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + ok is_toeplitz(sequence(4) + 5 - sequence(5)->dummy(0)), '4 x 5'; + ok is_toeplitz(sequence(5) + 4 - sequence(4)->dummy(0)), '5 x 4'; + + my $nt = sequence(4) + 5 - sequence(5)->dummy(0); + $nt->set(1, 4, 0); + ok !is_toeplitz($nt), 'one element failing'; + } + + done_testing; + exit; +} -- cgit From 2bcb7ffce5bfbf0c9f1e23ef714c678f9f992a8a Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 4 Apr 2023 22:31:34 +0200 Subject: Solution to task 2 --- challenge-211/jo-37/perl/ch-2.pl | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 challenge-211/jo-37/perl/ch-2.pl diff --git a/challenge-211/jo-37/perl/ch-2.pl b/challenge-211/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..dd2c6b03fb --- /dev/null +++ b/challenge-211/jo-37/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Prime::Util qw(forcomb lastfor); +use List::Util qw(sum); +use experimental qw(signatures); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <