diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-04-22 18:12:34 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-04-26 13:00:25 +0200 |
| commit | 159d11c615c94f52efb360452bf88bce5c920153 (patch) | |
| tree | 3c76c3db076981aa2571594b8a5c8e4b57565201 | |
| parent | b13afa65925584f7ae3fecad2750faf19f632d5e (diff) | |
| download | perlweeklychallenge-club-159d11c615c94f52efb360452bf88bce5c920153.tar.gz perlweeklychallenge-club-159d11c615c94f52efb360452bf88bce5c920153.tar.bz2 perlweeklychallenge-club-159d11c615c94f52efb360452bf88bce5c920153.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-266/jo-37/perl/ch-2.pl | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/challenge-266/jo-37/perl/ch-2.pl b/challenge-266/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..9e6de958e0 --- /dev/null +++ b/challenge-266/jo-37/perl/ch-2.pl @@ -0,0 +1,88 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [MATRIX] + +-examples + run the examples from the challenge + +-tests + run some tests + +MATRIX + a square matrix in any form accepted by the PDL string constructor, + e.g. '[ [1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1] ]' + +EOS + + +### Input and Output + +say +(qw(false true))[is_x("@ARGV")]; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/04/26/ch-266.html#task-2 + + +sub is_x { + my $m = pdl @_; + my ($on_x, $off_x) = $m->where_both(identity($m) | identity($m)->(-1:0)); + + all($on_x) && !any($off_x); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is is_x([ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], + ]), T(), 'example 1'; + + is is_x([ [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ]), F(), 'example 2'; + + is is_x([ [1, 0, 2], + [0, 3, 0], + [4, 0, 5], + ]), T(), 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is is_x([ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 0], + ]), F(), 'zero on X'; + + is is_x([ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 1], + [7, 0, 0, 1], + ]), F(), 'nonzero off X'; + } + + done_testing; + exit; +} |
