diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-05-06 11:47:33 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-05-10 14:37:26 +0200 |
| commit | 33244d28bc44a443eea17cbff0ec29b41f24e80d (patch) | |
| tree | 7f99c86a411061b3bada371df47d3b473abbe72e | |
| parent | eb6fa96130eaa2b93c9b20c7b8938e121889cea4 (diff) | |
| download | perlweeklychallenge-club-33244d28bc44a443eea17cbff0ec29b41f24e80d.tar.gz perlweeklychallenge-club-33244d28bc44a443eea17cbff0ec29b41f24e80d.tar.bz2 perlweeklychallenge-club-33244d28bc44a443eea17cbff0ec29b41f24e80d.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-268/jo-37/perl/ch-1.pl | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-268/jo-37/perl/ch-1.pl b/challenge-268/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..de0aa50546 --- /dev/null +++ b/challenge-268/jo-37/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [-tests] [--] [ListX ListY] + +-examples + run the examples from the challenge + +-tests + run some tests + +ListX ListY + two lists of comma separated numbers + +EOS + + +### Input and Output + +say magic_number(map [split /,/], @ARGV) // 'no magic number'; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/05/10/ch-268.html#task-1 + + +sub magic_number ($x, $y) { + my ($min, $max) = minmax pdl($y)->qsort - pdl($x)->qsort; + $min == $max ? $min : undef; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is magic_number([3, 7, 5], [9, 5, 7]), 2, 'example 1'; + is magic_number([1, 2, 1], [5, 4, 4]), 3, 'example 2'; + is magic_number([2], [5]), 3, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is magic_number([1, 2], [3, 5]), U(), 'no magic number'; + is magic_number([1, 2], [2, 1]), 0, 'same numbers'; + is magic_number([4, 3], [1, 2]), -2, 'negative magic number'; + } + + done_testing; + exit; +} |
