From 7fc9d00bc648a3047b554bd4a3f5d571333bc771 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Sun, 7 Sep 2025 19:49:53 -0600 Subject: Solve PWC338 --- challenge-338/wlmb/blog.txt | 1 + challenge-338/wlmb/perl/ch-1.pl | 19 +++++++++++++++++++ challenge-338/wlmb/perl/ch-2.pl | 25 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 challenge-338/wlmb/blog.txt create mode 100755 challenge-338/wlmb/perl/ch-1.pl create mode 100755 challenge-338/wlmb/perl/ch-2.pl diff --git a/challenge-338/wlmb/blog.txt b/challenge-338/wlmb/blog.txt new file mode 100644 index 0000000000..64398a8fac --- /dev/null +++ b/challenge-338/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2025/09/07/PWC338/ diff --git a/challenge-338/wlmb/perl/ch-1.pl b/challenge-338/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..d5eb7efa6e --- /dev/null +++ b/challenge-338/wlmb/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +# Perl weekly challenge 338 +# Task 1: Highest Row +# +# See https://wlmb.github.io/2025/09/07/PWC338/#task-1-highest-row +use v5.36; +use feature qw(try); +use PDL; +die <<~"FIN" unless @ARGV; + Usage: $0 M1 M2... + to find the maximum rowwise sum of the elements of the + matrices Mi. The inputs are strings of the form + "[[m00 m01...][m10 m11...]...]" which may be parsed + by pdl as a matrix + FIN +for(@ARGV){ + try{say "$_ -> ", pdl($_)->sumover->max} + catch($e){say $e} +} diff --git a/challenge-338/wlmb/perl/ch-2.pl b/challenge-338/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..f77b352676 --- /dev/null +++ b/challenge-338/wlmb/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +# Perl weekly challenge 338 +# Task 2: Max Distance +# +# See https://wlmb.github.io/2025/09/07/PWC338/#task-2-max-distance +use v5.36; +use feature qw(try); +use PDL; +use PDL::NiceSlice; +die <<~"FIN" unless @ARGV and @ARGV%2==0; + Usage: $0 X1 Y1 X2 Y2... + to find the maximum distance between the elements of array Xi and Yi. + The inputs Xi and Yi are strings that may be parsed by ~PDL~ as arrays + of the form "[z0 z1...]" + FIN +for my ($x, $y)(@ARGV){ + try { + # initialize $p and $q with the min and max of each input + my ($p, $q)=map {pdl(pdl($_)->minmax)} ($x,$y); + # reverse one array to subtract minimum from maximum and maximum from minimum + my $result = ($p-$q(-1:0))->abs->max; + say "$x, $y -> ", $result; + } + catch($e){ say $e } +} -- cgit