From 12398f27fd5355996518c91cef510d2d5e1008ac Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 11 Jan 2023 19:26:09 +0100 Subject: Solution to task 1 --- challenge-199/jo-37/perl/ch-1.pl | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 challenge-199/jo-37/perl/ch-1.pl diff --git a/challenge-199/jo-37/perl/ch-1.pl b/challenge-199/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..e3cbc9b6dc --- /dev/null +++ b/challenge-199/jo-37/perl/ch-1.pl @@ -0,0 +1,77 @@ +#!/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 < i having +# the same value. The second step goes from index j to index k < j +# having the same value again, where i shall be equal to k. The +# possible starting indices then are given by the nonzero elements in +# the diagonal of the product matrix. Actually, the diagonal has the +# number of solutions for every starting index and thus the sum over the +# diagonal is the requested number of good pairs. +# See +# https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-196/jo-37/perl/ch-1.pl +# for details. + +sub count_good_2 { + my $l = long @_; + # Matrix indicating pairs of equal values in the list. + my $eqt = ($l == $l->dummy(0)); + # Combining the value transition matrix and the index transition + # matrix by invalidating the upper right triangle values (including + # the diagonal) utilizing symmetry. + $eqt->where($l->sequence >= $l->sequence->dummy(0)) .= 0; + + # Chain the transition matrix with its transposed and sum over the + # diagonal. + ($eqt->xchg(0, 1) x $eqt)->diagonal(0, 1)->sum; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is count_good_2(1, 2, 3, 1, 1, 3), 4, 'example 1'; + is count_good_2(1, 2, 3), 0, 'example 2'; + is count_good_2(1, 1, 1, 1), 6, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} -- cgit From 055abc15e12b5c524043b122f0630ebd64e9e095 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 11 Jan 2023 19:26:22 +0100 Subject: Solution to task 2 --- challenge-199/jo-37/perl/ch-2.pl | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 challenge-199/jo-37/perl/ch-2.pl diff --git a/challenge-199/jo-37/perl/ch-2.pl b/challenge-199/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..292be9b25e --- /dev/null +++ b/challenge-199/jo-37/perl/ch-2.pl @@ -0,0 +1,85 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0 '!float'; +use PDL; +use experimental 'signatures'; + +our ($tests, $examples, $verbose); + +run_tests() if $tests || $examples; # does not return + +die < i having an absolute value difference not +# exceeding X. The second step goes from index j to index k > j having +# an absolute value difference not exceeding Y. The third step goes from +# index k to index l < k having an absolute value difference not +# exceeding Z, where i shall be equal to l. The possible starting +# indices then are given by the nonzero elements in the diagonal of the +# resulting product matrix. Actually, the diagonal has the number of +# solutions for every starting index and thus the sum over the diagonal +# is the requested number of good triplets. +# See +# https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-196/jo-37/perl/ch-1.pl +# for details. +sub count_good_3 ($x, $y, $z, @l) { + # Need a "double" piddle capable of holding 'inf' values. + my $l = pdl @l; + # Create the matrix of absolute pair differences. + my $adt = ($l - $l->dummy(0))->abs; + # Combining the difference matrix and the index transition matrix by + # invalidating the upper right triangle values (including the + # diagonal) utilizing symmetry. + $adt->where($l->sequence >= $l->sequence->dummy(0)) .= 'inf'; + + # Build transformation matrices and chain them. Then sum over the + # diagonal. + (($adt <= $z)->xchg(0, 1) x ($adt <= $y) x ($adt <= $x)) + ->diagonal(0, 1)->sum; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is count_good_3(qw(7 2 3 3 0 1 1 9 7)), 4, 'example 1'; + is count_good_3(qw(0 0 1 1 1 2 2 3)), 0, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is count_good_3(1, 1, 2, 0, 3, 1, 5, 2, 5), 1, 'single triplet'; + } + + done_testing; + exit; +} -- cgit From 61ab1108081ad9ab537efd7a5f35a1b136d67375 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Thu, 12 Jan 2023 18:16:37 +0100 Subject: Small update to challenge 196 task 1 --- challenge-196/jo-37/perl/ch-1.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-196/jo-37/perl/ch-1.pl b/challenge-196/jo-37/perl/ch-1.pl index 01310603d9..01fb22c449 100755 --- a/challenge-196/jo-37/perl/ch-1.pl +++ b/challenge-196/jo-37/perl/ch-1.pl @@ -94,10 +94,10 @@ sub find_132 { my $l = long @_; # Construct the comparison matrix: - my $comp = $l <=> $l->dummy(0); + my $comp = ($l <=> $l->dummy(0)); # Construct a lower triangular matrix indicating index order: - my $t = sequence(long, $l->dim(0)) < sequence(long, $l->dim(0))->dummy(0); + my $t = ($l->sequence < $l->sequence->dummy(0)); # Transition from the first to the second element: # The second must be larger than the first and have a larger index. -- cgit