aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-21 19:59:14 +0100
committerGitHub <noreply@github.com>2023-05-21 19:59:14 +0100
commit7a1f3b4dd17d80da30ea96742e5f1f38732ee677 (patch)
treefb7c62657fc1d2bd65b481234f86e661453a9d87
parent6570e4270510b05253b789e4fb09a8746a6ec47a (diff)
parent9eb9586e995aaee62632965f44422b26dbfa9bcd (diff)
downloadperlweeklychallenge-club-7a1f3b4dd17d80da30ea96742e5f1f38732ee677.tar.gz
perlweeklychallenge-club-7a1f3b4dd17d80da30ea96742e5f1f38732ee677.tar.bz2
perlweeklychallenge-club-7a1f3b4dd17d80da30ea96742e5f1f38732ee677.zip
Merge pull request #8115 from jo-37/contrib
Solutions to challenge 217
-rwxr-xr-xchallenge-217/jo-37/perl/ch-1.pl65
-rwxr-xr-xchallenge-217/jo-37/perl/ch-2.pl59
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;
+}