diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-14 16:57:16 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-17 14:43:47 +0100 |
| commit | ccc8bcd0901881922e4fa4178ccaddefbcb0e146 (patch) | |
| tree | b970eef48aea4edb1f0d9e6484e66d14f224d9db /challenge-208 | |
| parent | 0f4a809c0bead5e3fd8bc4616e1c011c9d6fb8a9 (diff) | |
| download | perlweeklychallenge-club-ccc8bcd0901881922e4fa4178ccaddefbcb0e146.tar.gz perlweeklychallenge-club-ccc8bcd0901881922e4fa4178ccaddefbcb0e146.tar.bz2 perlweeklychallenge-club-ccc8bcd0901881922e4fa4178ccaddefbcb0e146.zip | |
Solution to task 1
Diffstat (limited to 'challenge-208')
| -rwxr-xr-x | challenge-208/jo-37/perl/ch-1.pl | 75 |
1 files changed, 75 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; +} |
