diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-27 23:10:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-27 23:10:31 +0000 |
| commit | c0bd7896d08a8b06c077928ec9012e88e9b05e66 (patch) | |
| tree | f19b37c8dfaae2a6471c295a67d8ae2c825bcd6d | |
| parent | 3d02737b55d2a0192f967032f09928a05fdae39a (diff) | |
| parent | d3594f9f292c06c9f9b82e64348382783e29a01d (diff) | |
| download | perlweeklychallenge-club-c0bd7896d08a8b06c077928ec9012e88e9b05e66.tar.gz perlweeklychallenge-club-c0bd7896d08a8b06c077928ec9012e88e9b05e66.tar.bz2 perlweeklychallenge-club-c0bd7896d08a8b06c077928ec9012e88e9b05e66.zip | |
Merge pull request #12933 from wlmb/challenges
Solve PWC345
| -rw-r--r-- | challenge-345/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-345/wlmb/perl/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-345/wlmb/perl/ch-2.pl | 29 |
3 files changed, 57 insertions, 0 deletions
diff --git a/challenge-345/wlmb/blog.txt b/challenge-345/wlmb/blog.txt new file mode 100644 index 0000000000..11ee83e717 --- /dev/null +++ b/challenge-345/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2025/10/27/PWC345/ diff --git a/challenge-345/wlmb/perl/ch-1.pl b/challenge-345/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..7323bfcf84 --- /dev/null +++ b/challenge-345/wlmb/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +# Perl weekly challenge 345 +# Task 1: Peak Positions +# +# See https://wlmb.github.io/2025/10/27/PWC345/#task-1-peak-positions +use v5.36; +use feature qw(try); +use PDL; +use PDL::NiceSlice; + +die <<~"FIN" unless @ARGV; + Usage: $0 A0 A1... + to find the local maxima of the arrays An, + given by strings of the form + "[X0 X1...]" that may be parsed by PDL. + FIN +for(@ARGV){ + try { + my $ints=pdl($_); + # pad with smaller numbers at boundaries + my $padded=append(append($ints(0)-1, $ints), $ints(-1)-1); + my $indices=which(($padded>$padded->rotate(1))&($padded>$padded->rotate(-1))); + $indices -= 1; # remove left boundaries + say "$_ -> $indices"; + } + catch($e){warn $e;} +} diff --git a/challenge-345/wlmb/perl/ch-2.pl b/challenge-345/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..dbca6230af --- /dev/null +++ b/challenge-345/wlmb/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +# Perl weekly challenge 345 +# Task 2: Last Visitor +# +# See https://wlmb.github.io/2025/10/27/PWC345/#task-2-last-visitor +use v5.36; +use feature qw(try); +die <<~"FIN" unless @ARGV; + Usage: $0 A0 A1... + where An is a string with space separated numbers, positive + or -1. For each run of -1's output the previously seen positive + numbers, or -1 if they have been exhausted. + FIN +for(@ARGV){ + try { + my $count=0; # count of succesive -1's + my @answer = my @seen =(); + my @input = split " "; + for(@input){ + unshift(@seen, $_), $count=0, next if $_ > 0; + die "Only positive numbers and -1 are allowed: $_" + unless $_ == -1; + push @answer, $count<@seen? $seen[$count]:-1; + ++$count; + } + say "[$_] -> [@answer]"; + } + catch($e){warn $e;} +} |
