diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-26 23:26:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-26 23:26:53 +0100 |
| commit | a0a9f4164650b3bb1ec6066cbfa951e2fcb982a6 (patch) | |
| tree | 2469fc20d82fddf7aa4c867d8f898c86a41697c1 | |
| parent | 182bcd9945ea9f2c903d090bcc9898020c5e2b85 (diff) | |
| parent | f9b25d4d6faf6b1d5c5fe275e2205dbdec124d6b (diff) | |
| download | perlweeklychallenge-club-a0a9f4164650b3bb1ec6066cbfa951e2fcb982a6.tar.gz perlweeklychallenge-club-a0a9f4164650b3bb1ec6066cbfa951e2fcb982a6.tar.bz2 perlweeklychallenge-club-a0a9f4164650b3bb1ec6066cbfa951e2fcb982a6.zip | |
Merge pull request #12243 from wlmb/challenges
Solve PWC327
| -rw-r--r-- | challenge-327/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-327/wlmb/perl/ch-1.pl | 13 | ||||
| -rwxr-xr-x | challenge-327/wlmb/perl/ch-2.pl | 23 | ||||
| -rwxr-xr-x | challenge-327/wlmb/perl/ch-2a.pl | 27 |
4 files changed, 64 insertions, 0 deletions
diff --git a/challenge-327/wlmb/blog.txt b/challenge-327/wlmb/blog.txt new file mode 100644 index 0000000000..c2bbb1d465 --- /dev/null +++ b/challenge-327/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2025/06/26/PWC327/ diff --git a/challenge-327/wlmb/perl/ch-1.pl b/challenge-327/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..0b50d6f88b --- /dev/null +++ b/challenge-327/wlmb/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +# Perl weekly challenge 327 +# Task 1: Missing Integers +# +# See https://wlmb.github.io/2025/06/26/PWC327/#task-1-missing-integers +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 N1 N2..Nm + to find the numbers 1..m missing from N1 N2..Nm + FIN +my %present; +$present{$_}++ for @ARGV; +say "@ARGV -> ", join " ", grep {!$present{$_}} 1..@ARGV diff --git a/challenge-327/wlmb/perl/ch-2.pl b/challenge-327/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..56c9f55a03 --- /dev/null +++ b/challenge-327/wlmb/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +# Perl weekly challenge 327 +# Task 2: MAD +# +# See https://wlmb.github.io/2025/06/26/PWC327/#task-2-mad +use v5.36; +die <<~"FIN" unless @ARGV>=2; + Usage: $0 N1 N2... + to find the pair of integers Ni Nj separated by + the minimum absolute difference. + FIN +my $minimum=abs($ARGV[1]-$ARGV[0]); +my @result; +for my $i(0..@ARGV-1){ + my $x=$ARGV[$i]; + for my $j($i+1..@ARGV-1){ + my $y=$ARGV[$j]; + my $distance=abs($x-$y); + $minimum=$distance, @result=() if $distance < $minimum; + push @result, $x<$y? [$x,$y]:[$y,$x] if $distance==$minimum; + } +} +say "[@ARGV] -> [", (map{"[@$_]"} @result),"]" diff --git a/challenge-327/wlmb/perl/ch-2a.pl b/challenge-327/wlmb/perl/ch-2a.pl new file mode 100755 index 0000000000..eb29a5978e --- /dev/null +++ b/challenge-327/wlmb/perl/ch-2a.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +# Perl weekly challenge 327 +# Task 2: MAD +# +# PDL version +# See https://wlmb.github.io/2025/06/26/PWC327/#task-2-mad +use v5.36; +use PDL; +use PDL::NiceSlice; +die <<~"FIN" unless @ARGV>=2; + Usage: $0 A1 A2... + to find distinct pairs of integers [Ni Nj] taken from the array + Ap=[N1 N2...] separated by the minimum absolute difference. + The input should be in the form of strings "[N1 N2...]" which may + be interpreted as ndarrays by PDL. + FIN +for(@ARGV){ + my $p=pdl($_); # 1D array + say("Expected a 1D array: $_"), next unless $p->ndims==1; + my $n=$p->nelem; + say("Expected two or more elements: $_"), next unless $n>1; + my $pp=$p->dummy(0,$n); # 2D array + my $ad=($pp-$p)->abs; # abs difference + my $min=$ad->where($ad>0)->min; # MAD + my $pairs=pdl($pp,$pp->mv(0,1))->whereND($ad==$min); # select pairs + say "$p -> ",$pairs->whereND($pairs(:,0)<$pairs(:,1))->mv(0,1); # output distinct +} |
