aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-339/jo-37/perl/ch-2.pl92
1 files changed, 92 insertions, 0 deletions
diff --git a/challenge-339/jo-37/perl/ch-2.pl b/challenge-339/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..6f8658c2c1
--- /dev/null
+++ b/challenge-339/jo-37/perl/ch-2.pl
@@ -0,0 +1,92 @@
+#!/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;
+
+### 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 point
+
+ usage: $0 [-examples] [-tests] [--] [N...]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ N...
+ list of numbers
+
+ EOS
+}
+
+
+### Input and Output
+
+say peak_point(@ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/10/17/ch-339.html#task-2
+
+
+sub peak_point {
+ max cumusumover pdl 0, @_;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = peak_point(@$args);
+ is $result, $expected,
+ "$name: (@$args) -> $expected";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [[-5, 1, 5, -9, 2], 1, 'example 1'],
+ [[10, 10, 10, -25], 30, 'example 2'],
+ [[3, -4, 2, 5, -6, 1], 6, 'example 3'],
+ [[-1, -2, -3, -4], 0, 'example 4'],
+ [[-10, 15, 5], 10, 'example 5'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ plan 1;
+ pass 'no tests';
+ }) : pass 'skip tests';
+
+ exit;
+}