diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-09-09 22:43:37 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-09-12 08:24:11 +0200 |
| commit | cdb52146ea4f25587c77ef92ded5c12af2cbd864 (patch) | |
| tree | ea6a996ef1a7a417d30d199665c1a985abd9994d | |
| parent | 9b9bdc1cc754d1e7bbbefbd66bbe2339a47f4c00 (diff) | |
| download | perlweeklychallenge-club-cdb52146ea4f25587c77ef92ded5c12af2cbd864.tar.gz perlweeklychallenge-club-cdb52146ea4f25587c77ef92ded5c12af2cbd864.tar.bz2 perlweeklychallenge-club-cdb52146ea4f25587c77ef92ded5c12af2cbd864.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-338/jo-37/perl/ch-1.pl | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/challenge-338/jo-37/perl/ch-1.pl b/challenge-338/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..d064aa922c --- /dev/null +++ b/challenge-338/jo-37/perl/ch-1.pl @@ -0,0 +1,94 @@ +#!/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 - highest row + + usage: $0 [-examples] [-tests] [-verbose] [--] [M] + + -examples + run the examples from the challenge + + -tests + run some tests + + M + a matrix in a form like 'a,b,c;d,e,f' or '[a b c] [d e f]' + + EOS +} + + +### Input and Output + +say highest_row(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/09/12/ch-338.html#task-1 + + +sub highest_row { + max sumover pdl @_; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = highest_row(@$args); + is $result, $expected, + "$name: (@{[map qq([@$_]), @$args]}) -> $expected"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]], + 16, 'example 1'], + [[[1, 5], [7, 3], [3, 5]], 10, 'example 2'], + [[[1, 2, 3], [3, 2, 1]], 6, 'example 3'], + [[[2, 8, 7], [7, 1, 3], [1, 9, 5]], 17, 'example 4'], + [[[10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]], + 100, '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; +} |
