diff options
| -rw-r--r-- | challenge-268/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-268/jo-37/perl/ch-1.pl | 65 | ||||
| -rwxr-xr-x | challenge-268/jo-37/perl/ch-2.pl | 73 |
3 files changed, 139 insertions, 0 deletions
diff --git a/challenge-268/jo-37/blog.txt b/challenge-268/jo-37/blog.txt new file mode 100644 index 0000000000..bfd2908a61 --- /dev/null +++ b/challenge-268/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/05/10/ch-268.html 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; +} diff --git a/challenge-268/jo-37/perl/ch-2.pl b/challenge-268/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..9c4da0e2e2 --- /dev/null +++ b/challenge-268/jo-37/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; + +our ($tests, $examples, $num); +$num ||= 2; + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV && !(@ARGV % $num); +usage: $0 [-examples] [-tests] [-num=N] [--] [M] + +-examples + run the examples from the challenge + +-tests + run some tests + +-num=N + move N-tuples. Default: 2 + +M... + list of numbers with a multiple of N as size + +EOS + + +### Input and Output + +main: { + local $" = ', '; + say "(@{number_game($num, \@ARGV)})"; +} + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/05/10/ch-268.html#task-2 + + +sub number_game { + my $n = shift; + long(@_)->qsort->splitdim(0, $n)->(-1:0)->flat; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is number_game(2, [2, 5, 3, 4])->unpdl, [3, 2, 5, 4], 'example 1'; + is number_game(2, [9, 4, 1, 3, 6, 4, 6, 1])->unpdl, + [1, 1, 4, 3, 6, 4, 9, 6], 'example 2'; + is number_game(2, [1, 2, 2, 3])->unpdl, [2, 1, 3, 2], 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is number_game(3, [1 .. 12])->unpdl, + [3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10], 'triplets'; + + } + + done_testing; + exit; +} |
