diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-04-26 13:23:44 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-04-26 13:23:44 +0200 |
| commit | 2e5402954b41295ebb1a4eb7ac9df19fc306ebc1 (patch) | |
| tree | 6499c3c95f167b0e8c8c39dda150364e8a63267b | |
| parent | abdf407fb93a82f09995b16f3656f94df7985543 (diff) | |
| parent | 9e8d43cc014d6347082d65c6b5d060401e3e82f5 (diff) | |
| download | perlweeklychallenge-club-2e5402954b41295ebb1a4eb7ac9df19fc306ebc1.tar.gz perlweeklychallenge-club-2e5402954b41295ebb1a4eb7ac9df19fc306ebc1.tar.bz2 perlweeklychallenge-club-2e5402954b41295ebb1a4eb7ac9df19fc306ebc1.zip | |
Solutions to challenge 266
| -rw-r--r-- | challenge-266/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-266/jo-37/perl/ch-1.pl | 68 | ||||
| -rwxr-xr-x | challenge-266/jo-37/perl/ch-2.pl | 88 |
3 files changed, 157 insertions, 0 deletions
diff --git a/challenge-266/jo-37/blog.txt b/challenge-266/jo-37/blog.txt new file mode 100644 index 0000000000..93ee41023d --- /dev/null +++ b/challenge-266/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/04/26/ch-266.html diff --git a/challenge-266/jo-37/perl/ch-1.pl b/challenge-266/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..f636db4c2e --- /dev/null +++ b/challenge-266/jo-37/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl -s + + +use v5.24; +use Test2::V0; +use List::MoreUtils 'singleton'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [SENTENCE...] + +-examples + run the examples from the challenge + +-tests + run some tests + +SENTENCE + a list of sentences + +EOS + + +### Input and Output + +say "('", join("', '", uncommon_words(@ARGV)), "')"; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/04/26/ch-266.html#task-1 + + +sub uncommon_words { + singleton map /[[:alpha:]]+/g, @_; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is [uncommon_words('Mango is sweet', 'Mango is sour')], + ['sweet', 'sour'], 'example 1'; + + is [uncommon_words('Mango Mango', 'Orange')], + ['Orange'], 'example 2'; + + is [uncommon_words('Mango is Mango', 'Orange is Orange')], + [], 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is [uncommon_words('Twenty-two two-headed beasts headed east.')], + [qw(Twenty beasts east)], 'ignore non-alpha'; + } + + done_testing; + exit; +} 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; +} |
