diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-06 23:25:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-06 23:25:01 +0100 |
| commit | 288f6ae04ecb341b5f668fcde869d30cccd84cf8 (patch) | |
| tree | 68fb524549cb538b12d02c65ebd6e67459d94516 | |
| parent | bad6c23440a70a6016ead5f1f81303f091246f55 (diff) | |
| parent | 41433913638b88753218ca9dff6404438693fcf8 (diff) | |
| download | perlweeklychallenge-club-288f6ae04ecb341b5f668fcde869d30cccd84cf8.tar.gz perlweeklychallenge-club-288f6ae04ecb341b5f668fcde869d30cccd84cf8.tar.bz2 perlweeklychallenge-club-288f6ae04ecb341b5f668fcde869d30cccd84cf8.zip | |
Merge pull request #12132 from jo-37/contrib
Solutions to challenge 324
| -rw-r--r-- | challenge-324/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-324/jo-37/perl/ch-1.pl | 99 | ||||
| -rwxr-xr-x | challenge-324/jo-37/perl/ch-2.pl | 97 |
3 files changed, 197 insertions, 0 deletions
diff --git a/challenge-324/jo-37/blog.txt b/challenge-324/jo-37/blog.txt new file mode 100644 index 0000000000..2f0de06627 --- /dev/null +++ b/challenge-324/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2025/06/06/ch-324.html diff --git a/challenge-324/jo-37/perl/ch-1.pl b/challenge-324/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..223aaf0d15 --- /dev/null +++ b/challenge-324/jo-37/perl/ch-1.pl @@ -0,0 +1,99 @@ +#!/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; + +### Options and Arguments + +my ($tests, $examples, $verbose, $rows, $cols); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, + 'rows=i' => \$rows, + 'cols=i' => \$cols, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless $rows && $cols && @ARGV; + +sub usage { + die <<~EOS; + $0 - 2d array + + usage: $0 [-examples] [-tests] [-verbose] [-rows R -cols C N...] + + -examples + run the examples from the challenge + + -tests + run some tests + + -rows R + number of rows + + -cols C + number of columns + + N... + list of numbers + + EOS +} + + +### Input and Output + +say two_d_array($rows, $cols, @ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/06/06/ch-324.html#task-1 + + +sub two_d_array ($r, $c, @ints) { + pdl(@ints)->reshape($c, $r); +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = two_d_array(@$args); + pdl_is $result, $expected, + "$name: ($args->@[0, 1], ($args->@[2 .. $#$args])) -> $expected"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[2, 2, 1, 2, 3, 4], pdl([[1, 2], [3, 4]]), 'example 1'], + [[1, 3, 1, 2, 3], pdl([[1, 2, 3]]), 'example 2'], + [[4, 1, 1, 2, 3, 4], pdl([[1], [2], [3], [4]]), 'example 2'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 1; + pass 'no tests'; + }) : pass 'skip tests'; + + exit; +} diff --git a/challenge-324/jo-37/perl/ch-2.pl b/challenge-324/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..56b8069106 --- /dev/null +++ b/challenge-324/jo-37/perl/ch-2.pl @@ -0,0 +1,97 @@ +#!/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 - total xor + + 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 total_xor(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/06/06/ch-324.html#task-2 + +sub total_xor { + longlong(@_)->bor->shiftleft(@_ - 1); +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = total_xor(@$args); + is $result, $expected, + "$name: (@$args) -> $expected"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[1, 3], 6, 'example 1'], + [[5, 1, 6], 28, 'example 2'], + [[3 .. 8], 480, 'example 3'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 3; + is total_xor(1, 1, 1), 4, 'example from blog'; + is total_xor((1) x 32), 2**31, 'some more ones'; + + # cross check on random bits from random ints: + # perl -Mntheory=:all -E 'forcomb {$s = 0; $s ^= $ARGV[$_] for @_; $t += $s} @ARGV; say $t' 33600 541464 573584 557328 49616 525596 49372 34636 575368 575316 50640 1228 34112 1472 542020 557776 575112 557660 17988 33996 + is total_xor(33600, 541464, 573584, 557328, 49616, 525596, + 49372, 34636, 575368, 575316, 50640, 1228, 34112, 1472, + 542020, 557776, 575112, 557660, 17988, 33996), + 301702578176, 'cross check'; + }) : pass 'skip tests'; + + exit; +} |
