diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-10-31 10:06:43 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-10-31 10:06:43 +0100 |
| commit | 7acd1cd0a90bb7443604076ff181677db69ddf57 (patch) | |
| tree | c194217ed0dbf6188736515e3c619baf488d29f6 | |
| parent | 88a930c6a8de82b2c59f19958b4e1493ede4a4e0 (diff) | |
| parent | b01ec5029474288a03f07e9f3686e2f514b62e9e (diff) | |
| download | perlweeklychallenge-club-7acd1cd0a90bb7443604076ff181677db69ddf57.tar.gz perlweeklychallenge-club-7acd1cd0a90bb7443604076ff181677db69ddf57.tar.bz2 perlweeklychallenge-club-7acd1cd0a90bb7443604076ff181677db69ddf57.zip | |
Solutions to challenge 345
| -rw-r--r-- | challenge-345/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-345/jo-37/perl/ch-1.pl | 104 | ||||
| -rwxr-xr-x | challenge-345/jo-37/perl/ch-2.pl | 105 |
3 files changed, 210 insertions, 0 deletions
diff --git a/challenge-345/jo-37/blog.txt b/challenge-345/jo-37/blog.txt new file mode 100644 index 0000000000..be746dad34 --- /dev/null +++ b/challenge-345/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2025/10/31/ch-345.html diff --git a/challenge-345/jo-37/perl/ch-1.pl b/challenge-345/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..41ca6eaa23 --- /dev/null +++ b/challenge-345/jo-37/perl/ch-1.pl @@ -0,0 +1,104 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 qw(!float -no_srand); +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use PDL; +use PDL::NiceSlice; +use PDL::Stats::TS; + + +### Options and Arguments + +my ($tests, $examples, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV; + +sub usage { + die <<~EOS; + $0 - peak positions + + usage: $0 [-examples] [-tests] [--] [N...] + + -examples + run the examples from the challenge + + -tests + run some tests + + N... + list of integers + + EOS +} + + +### Input and Output + +say peak_positions(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/10/31/ch-345.html#task-1 + +sub peak_positions { + my $i = pdl -inf, @_, -inf; + my $s = ($i(1:-1) - $i(0:-2))->mult(1e6)->clip(-1, 1); + + which $s(0:-2) - $s(1:-1) == 2; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = peak_positions(@$args)->unpdl; + is $result, $expected, + "$name: (@$args) -> (@$expected)"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[1, 3, 2], [1], 'example 1'], + [[2, 4, 6, 5, 3], [2], 'example 2'], + [[1, 2, 3, 2, 4, 1], [2, 4], 'example 3'], + [[5, 3, 1], [0], 'example 4'], + [[1, 5, 1, 5, 1, 5, 1], [1, 3, 5], 'example 5'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + my @tests = ( + [[1, 2, 2, 1], [], 'no peak 1'], + [[1, 2, 2, 2, 1], [], 'no peak 2'], + [[2, 1, 1, 2], [0, 3], 'sink'], + ); + plan scalar @tests; + for (@tests) { + run_example @$_; + } + }) : pass 'skip tests'; + + exit; +} diff --git a/challenge-345/jo-37/perl/ch-2.pl b/challenge-345/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..5f3db285a5 --- /dev/null +++ b/challenge-345/jo-37/perl/ch-2.pl @@ -0,0 +1,105 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 qw(!float -no_srand); +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use PDL; +use PDL::NiceSlice; + + +### Options and Arguments + +my ($tests, $examples, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV; + +sub usage { + die <<~EOS; + $0 - last visitor + + usage: $0 [-examples] [-tests] [--] [N...] + + -examples + run the examples from the challenge + + -tests + run some tests + + N... + list of integers + + EOS +} + + +### Input and Output + +say last_visitor(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/10/31/ch-345.html#task-2 + +sub last_visitor { + my $ints = long @_; + my $p = $ints >= 0; + my $seen = append $ints(-1:0)->where_both($p(-1:0)); + my $len = $p->cumusumover; + + $seen($p(*1)->enumvec + $len(-1) - $len)->(!$p;?); +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name, $reason=undef) { + my $result = last_visitor(@$args)->unpdl; + my $todo; + $todo = todo $reason if $reason; + is $result, $expected, + "$name: (@$args) -> (@$expected)"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[5, -1, -1], [5, -1], 'example 1'], + [[3, 7, -1, -1, -1], [7, 3, -1], 'example 2'], + [[2, -1, 4, -1, -1], [2, 4, 2], 'example 3'], + [[10, 20, -1, 30, -1, -1], [20, 30, 20], 'example 4'], + [[-1, -1, 5, -1], [-1, -1, 5], 'example 5'], + ); + plan scalar @examples; + run_example @$_ for @examples; + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + my @tests = ( + [[-1, -1, -1], [-1, -1, -1], 'all -1'], + [[1, 2, 3], [], 'no -1'], + [[1, 0, 3, -1, -1], [3, 0], 'allow zero'], + [[1, 0, 3, -2, -3, -4, -5], [3, 0, 1, -5], + 'allow other negative numbers'], + ); + plan scalar @tests; + run_example @$_ for @tests; + }) : pass 'skip tests'; + + exit; +} |
