diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-01-10 21:45:42 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-01-12 17:04:08 +0100 |
| commit | f483f8bda6aa365c16c1d0410d25946df957d1d2 (patch) | |
| tree | f292c2c7a3447dc44cb9ce5f8d0f7fd7d266536c | |
| parent | 09a6c865607f509af50b9e62249babe849cfa538 (diff) | |
| download | perlweeklychallenge-club-f483f8bda6aa365c16c1d0410d25946df957d1d2.tar.gz perlweeklychallenge-club-f483f8bda6aa365c16c1d0410d25946df957d1d2.tar.bz2 perlweeklychallenge-club-f483f8bda6aa365c16c1d0410d25946df957d1d2.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-251/jo-37/perl/ch-2.pl | 79 |
1 files changed, 79 insertions, 0 deletions
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; +} |
