diff options
| -rw-r--r-- | challenge-337/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-337/jo-37/perl/ch-1.pl | 102 | ||||
| -rwxr-xr-x | challenge-337/jo-37/perl/ch-2.pl | 118 |
3 files changed, 221 insertions, 0 deletions
diff --git a/challenge-337/jo-37/blog.txt b/challenge-337/jo-37/blog.txt new file mode 100644 index 0000000000..171dd50039 --- /dev/null +++ b/challenge-337/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2025/09/05/ch-337.html diff --git a/challenge-337/jo-37/perl/ch-1.pl b/challenge-337/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..88b3897405 --- /dev/null +++ b/challenge-337/jo-37/perl/ch-1.pl @@ -0,0 +1,102 @@ +#!/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 - smaller than current + + 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 smaller_than_current(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/09/05/ch-337.html#task-1 + + +sub smaller_than_current { + my $num = pdl @_; + my $sort = $num($num->qsorti); + $sort .= $sort(-1:0)->dummy(0)->enumvec->(-1:0); + $sort += sequence $num; + $num; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = smaller_than_current(@$args)->unpdl; + is $result, $expected, + "$name: (@$args) -> (@$expected)"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[6, 5, 4, 8], [2, 1, 0, 3], 'example 1'], + [[7, 7, 7, 7], [3, 3, 3, 3], 'example 2'], + [[5, 4, 3, 2, 1], [4, 3, 2, 1, 0], 'example 3'], + [[-1, 0, 3, -2, 1], [1, 2, 4, 0, 3], 'example 4'], + [[0, 1, 1, 2, 0], [1, 3, 3, 4, 1], 'example 5'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + my @tests = ( + [[4,1,3,1,4,1,3,1,3,1], [9,4,7,4,9,4,7,4,7,4], 'repeated values'], + ); + plan scalar @tests; + for (@tests) { + run_example @$_; + } + }) : pass 'skip tests'; + + exit; +} diff --git a/challenge-337/jo-37/perl/ch-2.pl b/challenge-337/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..0354cbc06d --- /dev/null +++ b/challenge-337/jo-37/perl/ch-2.pl @@ -0,0 +1,118 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 qw(!float -no_srand); +use Test2::Tools::Subtest 'subtest_streamed'; +use Test2::Tools::PDL; +use Getopt::Long; +use experimental 'signatures'; + +use PDL; +use PDL::NiceSlice; + + +### Options and Arguments + +my ($tests, $examples, $verbose, $col, $row); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, + 'col=i' => \$col, + 'row=i' => \$row, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV && $col > 0 && $row > 0; + +sub usage { + die <<~EOS; + $0 - odd matrix + + usage: $0 [-examples] [-tests] [-verbose] [--] [-col COLS -row ROWS LOC...] + + -examples + run the examples from the challenge + + -tests + run some tests + + -col COLS + create matrix with COLS columns + + -row ROWS + create matrix with ROWS rows + + LOC... + list of locations in a form like + '[R,C],...' or 'R,C;...' + + EOS +} + + +### Input and Output + +say odd_matrix($row, $col, @ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/09/05/ch-337.html#task-2 + + +sub odd_matrix ($row, $col, @loc) { + my $m = zeroes long, $col, $row; + my ($freq, $indx) = indx(@loc)->xchg(0,1)->qsort->rle; + $freq->inplace->setvaltobad(0); + $indx->inplace->copybad($freq); + $m(,$indx(,(0))) .= $freq(,(0))->dummy(0); + $m($indx(,(1))) += $freq(,(1)); + $m; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = odd_matrix(@$args); + my $exp_pdl = indx $expected; + pdl_is $result, $exp_pdl, + qq($name: $args->@[0,1], (@{[map "[@$_]", $args->@[2..$#$args]]}) -> $exp_pdl); + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[2, 3 ,=> [0,1],[1,1]], [[1,3,1],[1,3,1]], 'example 1'], + [[2, 2 ,=> [1,1],[0,0]], [[2,2],[2,2]], 'example 2'], + [[3, 3 ,=> [0,0],[1,2],[2,1]], [[2,2,2],[2,2,2],[2,2,2]], 'example 3'], + [[1, 5 ,=> [0,2],[0,4]], [[2,2,3,2,3]], 'example 4'], + [[4, 2 ,=> [1,0],[3,1],[2,0],[0,1]], [[3,3],[3,3],[3,3],[3,3]], 'example 5'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + my @tests = ( + [[2, 2 ,=> [0,0],[0,0]], [[4,2],[2,0]], 'non-unique loc'], + [[4, 3 ,=> [3,1],[2,1],[3,2],[0, 1]], + [[1,4,2],[0,3,1],[1,4,2],[2,5,3]], 'example from blog'] + ); + plan scalar @tests; + for (@tests) { + run_example @$_; + } + }) : pass 'skip tests'; + + exit; +} |
