aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-208/jo-37/perl/ch-1.pl75
-rwxr-xr-xchallenge-208/jo-37/perl/ch-2.pl62
2 files changed, 137 insertions, 0 deletions
diff --git a/challenge-208/jo-37/perl/ch-1.pl b/challenge-208/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..218bc3e9d4
--- /dev/null
+++ b/challenge-208/jo-37/perl/ch-1.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use List::UtilsBy 'min_by';
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 2;
+usage: $0 [-examples] [-tests] [LIST1 LIST2]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+LIST1 LIST2
+ two lists of comma separated words
+
+EOS
+
+
+### Input and Output
+
+say "(@{[min_ind_sum(map [split /,/, $_], @ARGV)]})";
+
+
+### Implementation
+
+sub min_ind_sum ($x, $y) {
+ # Build key-value pairs from the strings in @$y and their index
+ # position.
+ my %hy;
+ @hy{@$y} = (0 .. $#$y);
+
+ # Build key-value pairs from the strings in @$x and their index sum
+ # within both arrays.
+ my %hx;
+ @hx{@$x} = do {
+ # Add the index positions in @$x and @$y for common strings.
+ # Strings in @$x that are missing in @$y produce infinity.
+ my $i = 0;
+ map $i++ + ($_ // 'inf'), delete @hy{@$x};
+ };
+
+ # Select all elements from @$x having a finite minimum index sum.
+ min_by {$hx{$_}} grep $hx{$_} < 'inf', @$x;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [min_ind_sum(["Perl", "Raku", "Love"], ["Raku", "Perl", "Hate"])],
+ ["Perl", "Raku"], 'example 1';
+ is [min_ind_sum(["A", "B", "C"], ["D", "E", "F"])],
+ [], 'example 2';
+ is [min_ind_sum(["A", "B", "C"], ["C", "A", "B"])],
+ ["A"], 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-208/jo-37/perl/ch-2.pl b/challenge-208/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..723db1de46
--- /dev/null
+++ b/challenge-208/jo-37/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use List::Util qw(uniqint sum);
+
+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
+
+main: {
+ my ($dup, $miss) = dup_miss(@ARGV);
+ say $dup || $miss ? "($dup,$miss)" : -1;
+}
+
+
+### Implementation
+
+sub dup_miss {
+ # - A single duplicate value is the difference between the sum over
+ # all elements and the sum over the unique elements.
+ # - A single missing value is the difference between the sum over
+ # all expected elements and the sum over the unique elements.
+ my $usum = sum uniqint @_;
+ map $_ - $usum, sum(@_), @_ * (@_ + 1) / 2;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ is [dup_miss(1, 2, 2, 4)], [2, 3], 'example 1';
+ is [dup_miss(1, 2, 3, 4)], [0, 0], 'example 2';
+ is [dup_miss(1, 2, 3, 3)], [3, 4], 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}