aboutsummaryrefslogtreecommitdiff
path: root/challenge-339
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-15 23:07:19 +0100
committerGitHub <noreply@github.com>2025-09-15 23:07:19 +0100
commitde543de02dfe1576d5bb84ac062ba6c6cf745a06 (patch)
treeca2d7e502c1b6c286ba27bf3959ff94d3ed9ff7c /challenge-339
parent1c91bb9e79ef591269c8cf334cbbde86433b40c8 (diff)
parent5e9b9caf403ffb066d900d120dcaf409dc1c4e7a (diff)
downloadperlweeklychallenge-club-de543de02dfe1576d5bb84ac062ba6c6cf745a06.tar.gz
perlweeklychallenge-club-de543de02dfe1576d5bb84ac062ba6c6cf745a06.tar.bz2
perlweeklychallenge-club-de543de02dfe1576d5bb84ac062ba6c6cf745a06.zip
Merge pull request #12683 from wlmb/challenges
Solve PWC339
Diffstat (limited to 'challenge-339')
-rw-r--r--challenge-339/wlmb/blog.txt1
-rwxr-xr-xchallenge-339/wlmb/perl/ch-1.pl32
-rwxr-xr-xchallenge-339/wlmb/perl/ch-2.pl22
3 files changed, 55 insertions, 0 deletions
diff --git a/challenge-339/wlmb/blog.txt b/challenge-339/wlmb/blog.txt
new file mode 100644
index 0000000000..f7ab3ed297
--- /dev/null
+++ b/challenge-339/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2025/09/15/PWC339/
diff --git a/challenge-339/wlmb/perl/ch-1.pl b/challenge-339/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..77066bffd6
--- /dev/null
+++ b/challenge-339/wlmb/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 339
+# Task 1: Max Diff
+#
+# See https://wlmb.github.io/2025/09/15/PWC339/#task-1-max-diff
+use v5.36;
+use feature qw(try);
+use PDL;
+use PDL::NiceSlice;
+die <<~"FIN" unless @ARGV;
+ Usage: $0 A0 A1...
+ to find the maximum difference between products of disctinct
+ elements of the arrays A0, A1... Each array is input as a string
+ of the form "[X0 X1...]" that can be read by PDL
+ FIN
+for(@ARGV){
+ try {
+ my $in=pdl($_);
+ die "Expected four or more numbers" unless $in->dim(0)>=4;
+ my $products=$in*$in(*1);
+ my $diffs=$products-$products(*1,*1); # Xi*Xj-Xk*Xl
+ my ($i, $j, $k, $l) = $diffs->ndcoords->mv(0,-1)->dog; #coordinates into ndarray $diffs
+ $diffs=$diffs->where(
+ ($i!=$j)&($i!=$k)&($i!=$l)
+ &($j!=$k)&($j!=$l)
+ &($k!=$l)); # select elements with no repeating coordinates
+ say "$_ -> ", $diffs->max;
+ }
+ catch($e){
+ warn $e;
+ }
+}
diff --git a/challenge-339/wlmb/perl/ch-2.pl b/challenge-339/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..e642cfe3c3
--- /dev/null
+++ b/challenge-339/wlmb/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 339
+# Task 2: Peak Point
+#
+# See https://wlmb.github.io/2025/09/15/PWC339/#task-2-peak-point
+use v5.36;
+use feature qw(try);
+use PDL;
+die <<~"FIN" unless @ARGV;
+ Usage: $0 G0 G1...
+ to find the maximum value of the cumulative sums of the gains
+ in arrays G0, G1... Each argument is a string of the form "[g0 g1...]"
+ that may be understood by PDL, where gn is the gain in step n.
+ FIN
+for(@ARGV){
+ try{
+ say "$_ -> ", append(0,pdl($_))->cumusumover->max
+ }
+ catch($e){
+ warn $e;
+ }
+}