aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-23 20:49:52 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-26 15:38:40 +0100
commite694632f0080c5c498492298bd4d732047178617 (patch)
tree6ef9b353f588ce96f4350c0ea683e4ee34ae875a
parentcaef8be004348b8426f7694d75a0f28eeae8b493 (diff)
downloadperlweeklychallenge-club-e694632f0080c5c498492298bd4d732047178617.tar.gz
perlweeklychallenge-club-e694632f0080c5c498492298bd4d732047178617.tar.bz2
perlweeklychallenge-club-e694632f0080c5c498492298bd4d732047178617.zip
Solution to task 2
-rwxr-xr-xchallenge-253/jo-37/perl/ch-2.pl75
1 files changed, 75 insertions, 0 deletions
diff --git a/challenge-253/jo-37/perl/ch-2.pl b/challenge-253/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..dad11ed26a
--- /dev/null
+++ b/challenge-253/jo-37/perl/ch-2.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use PDL;
+use PDL::NiceSlice;
+
+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
+ a matrix in any form accepted by the PDL string constructor,
+ e.g.
+ "[[1, 2, 0, 0, 4], [1, 1, 1, 1, 0], [3, 0, 0, 0, 0]]"
+EOS
+
+
+### Input and Output
+
+say weakest("@ARGV");
+
+
+### Implementation
+
+sub weakest {
+ my $m = pdl @_;
+ $m->append($m((0))->ndcoords)->qsortveci;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is weakest([
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 0],
+ [1, 0, 0, 0, 0],
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 1]
+ ])->unpdl, [2, 0, 3, 1, 4], 'example 1';
+
+ is weakest([
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]
+ ])->unpdl, [0, 2, 3, 1], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is weakest([0, 1, 2, 3],
+ [1, 0, 0, 0],
+ [1, 2, 3, 4],
+ [0, 1, 2, 3],
+ [1, 2, 3, 5])->unpdl, [0, 3, 1, 2, 4], 'arbitrary matrix';
+ }
+
+ done_testing;
+ exit;
+}