aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-05-13 17:01:25 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-05-17 15:10:32 +0200
commitc58f4f98262169e4c91f895a40c1d738b628c504 (patch)
tree22eb0bb3d1f46693c078098d33f9af0e78586b3f
parent028dd3b3ee3ce5cd0fa22e00d8d0b3aeceb6590a (diff)
downloadperlweeklychallenge-club-c58f4f98262169e4c91f895a40c1d738b628c504.tar.gz
perlweeklychallenge-club-c58f4f98262169e4c91f895a40c1d738b628c504.tar.bz2
perlweeklychallenge-club-c58f4f98262169e4c91f895a40c1d738b628c504.zip
Solution to task 2
-rwxr-xr-xchallenge-269/jo-37/perl/ch-2.pl64
1 files changed, 64 insertions, 0 deletions
diff --git a/challenge-269/jo-37/perl/ch-2.pl b/challenge-269/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..ba82d29b33
--- /dev/null
+++ b/challenge-269/jo-37/perl/ch-2.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use POSIX qw(Inf NaN);
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [I...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+I...
+ list of integers
+
+EOS
+
+
+### Input and Output
+
+say "(@{[join ', ', de(@ARGV)]})";
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/05/17/ch-269.html#task-2
+
+
+sub de {
+ my @arr;
+ push @{$arr[($arr[0][-1] // NaN) <= ($arr[1][-1] // Inf)]}, $_ for @_;
+ map @$_, @arr;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [de(2, 1, 3, 4, 5)], [2, 3, 4, 5, 1], 'example 1';
+ is [de(3, 2, 4)], [3, 4, 2], 'example 2';
+ is [de(5, 4, 3, 8)], [5, 3, 4, 8], 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is [de(2, 1, 1, 3)], [2, 1, 1, 3], 'not unique';
+
+ }
+
+ done_testing;
+ exit;
+}