aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-12 10:21:32 +0100
committerGitHub <noreply@github.com>2025-09-12 10:21:32 +0100
commit92a32cb77bd007dd3f5d414345843720b76b60c5 (patch)
treeabe254be68f53cf0df0a4537e186742c2dd21ce1
parent9b9bdc1cc754d1e7bbbefbd66bbe2339a47f4c00 (diff)
parent97d427e408fb91b7351d49f32cc5b8713b954791 (diff)
downloadperlweeklychallenge-club-92a32cb77bd007dd3f5d414345843720b76b60c5.tar.gz
perlweeklychallenge-club-92a32cb77bd007dd3f5d414345843720b76b60c5.tar.bz2
perlweeklychallenge-club-92a32cb77bd007dd3f5d414345843720b76b60c5.zip
Merge pull request #12665 from jo-37/contrib
Solutions to challenge 338
-rw-r--r--challenge-338/jo-37/blog.txt1
-rwxr-xr-xchallenge-338/jo-37/perl/ch-1.pl94
-rwxr-xr-xchallenge-338/jo-37/perl/ch-2.pl108
3 files changed, 203 insertions, 0 deletions
diff --git a/challenge-338/jo-37/blog.txt b/challenge-338/jo-37/blog.txt
new file mode 100644
index 0000000000..5531c9693b
--- /dev/null
+++ b/challenge-338/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2025/09/12/ch-338.html
diff --git a/challenge-338/jo-37/perl/ch-1.pl b/challenge-338/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..d064aa922c
--- /dev/null
+++ b/challenge-338/jo-37/perl/ch-1.pl
@@ -0,0 +1,94 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 qw(!float -no_srand);
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+use PDL;
+
+### Options and Arguments
+
+my ($tests, $examples, $verbose);
+GetOptions(
+ 'examples!' => \$examples,
+ 'tests!' => \$tests,
+ 'verbose!' => \$verbose,
+) or usage();
+
+run_tests($examples, $tests); # tests do not return
+
+usage() unless @ARGV;
+
+sub usage {
+ die <<~EOS;
+ $0 - highest row
+
+ usage: $0 [-examples] [-tests] [-verbose] [--] [M]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ M
+ a matrix in a form like 'a,b,c;d,e,f' or '[a b c] [d e f]'
+
+ EOS
+}
+
+
+### Input and Output
+
+say highest_row(@ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/09/12/ch-338.html#task-1
+
+
+sub highest_row {
+ max sumover pdl @_;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = highest_row(@$args);
+ is $result, $expected,
+ "$name: (@{[map qq([@$_]), @$args]}) -> $expected";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [[[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]],
+ 16, 'example 1'],
+ [[[1, 5], [7, 3], [3, 5]], 10, 'example 2'],
+ [[[1, 2, 3], [3, 2, 1]], 6, 'example 3'],
+ [[[2, 8, 7], [7, 1, 3], [1, 9, 5]], 17, 'example 4'],
+ [[[10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]],
+ 100, 'example 5'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ plan 1;
+ pass 'no tests';
+ }) : pass 'skip tests';
+
+ exit;
+}
diff --git a/challenge-338/jo-37/perl/ch-2.pl b/challenge-338/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..1861f7c460
--- /dev/null
+++ b/challenge-338/jo-37/perl/ch-2.pl
@@ -0,0 +1,108 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 qw(!float -no_srand);
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+use PDL;
+use PDL::NiceSlice;
+
+### Options and Arguments
+
+my ($tests, $examples, $verbose);
+GetOptions(
+ 'examples!' => \$examples,
+ 'tests!' => \$tests,
+ 'verbose!' => \$verbose,
+) or usage();
+
+run_tests($examples, $tests); # tests do not return
+
+usage() unless @ARGV;
+
+sub usage {
+ die <<~EOS;
+ $0 - max distance
+
+ usage: $0 [-examples] [-tests] [-verbose] [--] [L...]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ L...
+ list of integer lists in a form like 'a,b,c;d,e' or '[a b c] [d e]'
+
+ EOS
+}
+
+
+### Input and Output
+
+say max_distance(@ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/09/12/ch-338.html#task-2
+
+sub max_distance {
+ state $diff = long -1, 1;
+ state $sel = indx [0, 1, 0], [0, 0, 1];
+ $PDL::undefval = badvalue long;
+ (my $m = long @_)->badflag(1);
+ my $mm = cat +($m->minmaximum)[0, 1];
+ my $mm2 = maximum_n_ind $mm * $diff(*1), 2;
+ my $cand = $mm2->cat($mm->index1d($mm2))
+ ->index1d($sel)->xchg(0, 1)->inner($diff);
+
+ $cand(,1)->where($cand(,0))->max;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = max_distance(@$args);
+ is $result, $expected,
+ "$name: (@{[map qq([@$_]), @$args]}) -> $expected";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [[[4, 5, 7], [9, 1, 3, 4]], 6, 'example 1'],
+ [[[2, 3, 5, 4], [3, 2, 5, 5, 8, 7]], 6, 'example 2'],
+ [[[2, 1, 11, 3], [2, 5, 10, 2]], 9, 'example 3'],
+ [[[1, 2, 3], [3, 2, 1]], 2, 'example 4'],
+ [[[1, 0, 2, 3], [5, 0]], 5, 'example 5'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my @tests = (
+ [[[2, 5, 10], [5, 6, 7], [1, 4, 9]], 9, 'max_1/min_1'],
+ [[[1, 5, 10], [5, 6, 7], [2, 4, 7]], 8, 'max_1/min_2'],
+ [[[1, 5, 10], [5, 6, 7], [3, 4, 9]], 8, 'max_2/min_1'],
+ );
+ plan scalar @tests;
+ for (@tests) {
+ run_example @$_;
+ }
+ }) : pass 'skip tests';
+
+ exit;
+}