aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2025-09-15 11:16:46 -0600
committerLuis Mochan <mochan@fis.unam.mx>2025-09-15 11:16:46 -0600
commit0d08beea92cbe83cdce61a6f56ea40638af6c5b1 (patch)
treec300cd60a5089bcff043ed148bf9109394c850d0
parentb2cc80b5507fc4f13b2eec8050f70ef12019ffb6 (diff)
downloadperlweeklychallenge-club-0d08beea92cbe83cdce61a6f56ea40638af6c5b1.tar.gz
perlweeklychallenge-club-0d08beea92cbe83cdce61a6f56ea40638af6c5b1.tar.bz2
perlweeklychallenge-club-0d08beea92cbe83cdce61a6f56ea40638af6c5b1.zip
Solve PWC339
-rw-r--r--challenge-339/wlmb/blog.txt1
-rwxr-xr-xchallenge-339/wlmb/perl/ch-1.pl31
-rwxr-xr-xchallenge-339/wlmb/perl/ch-2.pl22
3 files changed, 54 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..4a51a15470
--- /dev/null
+++ b/challenge-339/wlmb/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/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($_);
+ 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..9a81fa78a3
--- /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;
+ }
+}