diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-01-12 17:04:44 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-01-12 17:04:44 +0100 |
| commit | 45c35d5cd46832d9b57407a7e7c79f427606c4f8 (patch) | |
| tree | 7f8cf1d0562085094024d2691e3a8175e5c91e89 | |
| parent | 6e26b5e5f2133daa14b7ee59adef9a8977b523fe (diff) | |
| parent | b278047e47d0ec05f940110181bf12a3f5f2e541 (diff) | |
| download | perlweeklychallenge-club-45c35d5cd46832d9b57407a7e7c79f427606c4f8.tar.gz perlweeklychallenge-club-45c35d5cd46832d9b57407a7e7c79f427606c4f8.tar.bz2 perlweeklychallenge-club-45c35d5cd46832d9b57407a7e7c79f427606c4f8.zip | |
Solutions to challenge 251
| -rw-r--r-- | challenge-251/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-251/jo-37/perl/ch-1.pl | 58 | ||||
| -rwxr-xr-x | challenge-251/jo-37/perl/ch-2.pl | 79 |
3 files changed, 138 insertions, 0 deletions
diff --git a/challenge-251/jo-37/blog.txt b/challenge-251/jo-37/blog.txt new file mode 100644 index 0000000000..f69aedfe0b --- /dev/null +++ b/challenge-251/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/blog/pwc/challenge-251 diff --git a/challenge-251/jo-37/perl/ch-1.pl b/challenge-251/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..11402fc894 --- /dev/null +++ b/challenge-251/jo-37/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +N... + list of non-negative integers + +EOS + + +### Input and Output + +say concat_val(@ARGV); + + +### Implementation + +sub concat_val { + my $val = 0; + while (@_) { + $val += shift() . (pop() // ''); + } + $val; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is concat_val(6, 12, 25, 1), 1286, 'example 1'; + is concat_val(10, 7, 31, 5, 2, 2), 489, 'example 2'; + is concat_val(1, 2, 10), 112, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-251/jo-37/perl/ch-2.pl b/challenge-251/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..aa771f7f27 --- /dev/null +++ b/challenge-251/jo-37/perl/ch-2.pl @@ -0,0 +1,79 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [M] + +-examples + run the examples from the challenge + +-tests + run some tests + +M + matrix in any form accepted by the PDL constructor, + e.g. "3,7,8;9,11,13;15,16,17" + +EOS + + +### Input and Output + +say lucky_number("@ARGV"); + + +### Implementation + +sub lucky_number { + my $m = pdl @_; + my $minmax = $m->xchg(0, 1)->maxover->minimum; + $minmax == $m->minover->maximum ? $minmax : -1; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is lucky_number( + [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17]), 15, 'example 1'; + + is lucky_number( + [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12]), 12, 'example 2'; + + is lucky_number( + [7 ,8], + [1 ,2]), 7, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is lucky_number( + [2, 7, 5], + [6, 1, 8], + [3, 9, 4]), -1, 'none'; + + # not unique: + is lucky_number( + [2, 3, 3], + [1, 2, 3], + [1, 1, 2]), 2, 'only one three twos is lucky'; + } + + done_testing; + exit; +} |
