diff options
| -rwxr-xr-x | challenge-217/jo-37/perl/ch-1.pl | 65 | ||||
| -rwxr-xr-x | challenge-217/jo-37/perl/ch-2.pl | 59 |
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-217/jo-37/perl/ch-1.pl b/challenge-217/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..4c43a4b8ca --- /dev/null +++ b/challenge-217/jo-37/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [-verbose] [--] [M...] + +-examples + run the examples from the challenge + +-tests + run some tests + +M... + a matrix in any form accepted by the PDL string contructor, e.g. + "[1 2 3][4 5 6]" + +EOS + + +### Input and Output + +say third_smallest(pdl "@ARGV"); + + +### Implementation + +# There is no need to sort the matrix. Just pick the third smallest +# value's index and return the value. +sub third_smallest ($p) { + minimum_n_ind $p->flat, my $min = zeroes indx, 3; + $p->flat->($min(-1))->sclr; +} + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is third_smallest(pdl([3, 1, 2], [5, 2, 4], [0, 1, 3])), 1, + 'example 1'; + + is third_smallest(pdl([2, 1], [4, 5])), 4, + 'example 2'; + + is third_smallest(pdl([1, 0, 3], [0, 0, 0], [1, 2, 1])), 0, + 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-217/jo-37/perl/ch-2.pl b/challenge-217/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..f6145ffd7b --- /dev/null +++ b/challenge-217/jo-37/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/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 numbers + +EOS + + +### Input and Output + +say maximum_number(@ARGV); + + +### Implementation + +# At first glance it looks as the reverse lexicographic order would +# solve the task, but this is disproved by example 5. Thus creating a +# "reverse concatenation order". +sub maximum_number { + join '', sort {$b . $a <=> $a . $b} @_; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is maximum_number(1, 23), 231, 'example 1'; + is maximum_number(10, 3, 2), 3210, 'example 2'; + is maximum_number(31, 2, 4, 10), 431210, 'example 3'; + is maximum_number(5, 11, 4, 1, 2), 542111, 'example 4'; + is maximum_number(1, 10), 110, 'example 5'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
