aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-01-13 13:48:43 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-01-13 13:48:43 +0100
commit84e0badde90338ff63e65b7eb54b8d1d405f813a (patch)
treed4a2ae8828696684db222b062d633b33b8354514
parent13e4a2c007d7a0aac2bd1b8a48aa28003d772dc5 (diff)
parent61ab1108081ad9ab537efd7a5f35a1b136d67375 (diff)
downloadperlweeklychallenge-club-84e0badde90338ff63e65b7eb54b8d1d405f813a.tar.gz
perlweeklychallenge-club-84e0badde90338ff63e65b7eb54b8d1d405f813a.tar.bz2
perlweeklychallenge-club-84e0badde90338ff63e65b7eb54b8d1d405f813a.zip
Solutions to challenge 199
-rwxr-xr-xchallenge-196/jo-37/perl/ch-1.pl4
-rwxr-xr-xchallenge-199/jo-37/perl/ch-1.pl77
-rwxr-xr-xchallenge-199/jo-37/perl/ch-2.pl85
3 files changed, 164 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.
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 <<EOS if @ARGV < 2;
+usage: $0 [-examples] [-tests] [N1 N2...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N1 N2...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say count_good_2(@ARGV);
+
+
+### Implementation
+
+# This task may be regarded as a simplified variant of challenge 196/1.
+# The first transformation step is from index i to index j > 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;
+}
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 <<EOS if @ARGV < 5;
+usage: $0 [-examples] [-tests] [X Y Z N1 N2...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+X Y Z
+ limits for absolute pair differences
+
+N1 N2...
+ list of values
+EOS
+
+
+### Input and Output
+
+say count_good_3(@ARGV);
+
+
+### Implementation
+
+# This task may be regarded as a variant of challenge 196/1 and is very
+# similar to this week's task 1. The first transformation step is from
+# index i to index j > 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;
+}