diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-04-19 16:02:22 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-04-19 16:15:02 +0200 |
| commit | 2eafcc9024af4a1facd5087216b457db5d7ed8cd (patch) | |
| tree | aa3b1e544b48f0fc1680deaeb3657316fb50e382 | |
| parent | 7ab9d49b5c4b09440647e714d5b8510a148a4063 (diff) | |
| download | perlweeklychallenge-club-2eafcc9024af4a1facd5087216b457db5d7ed8cd.tar.gz perlweeklychallenge-club-2eafcc9024af4a1facd5087216b457db5d7ed8cd.tar.bz2 perlweeklychallenge-club-2eafcc9024af4a1facd5087216b457db5d7ed8cd.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-265/jo-37/perl/ch-2.pl | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/challenge-265/jo-37/perl/ch-2.pl b/challenge-265/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..56929d721f --- /dev/null +++ b/challenge-265/jo-37/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::AllUtils qw(pairmap min_by count_by); +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [TARGET WORD...] + +-examples + run the examples from the challenge + +-tests + run some tests + +TARGET + target string + +WORD... + list of words + +EOS + + +### Input and Output + +say completing_word(@ARGV); + + +### Implementation + +sub completing_word ($str, @str) { + my $target = qr{ + ^ + @{[ + pairmap { '(?=' . ".*?$a" x $b . ')'} + count_by {$_} + lc($str) =~ /([[:alpha:]])/g + ]} + [[:alpha:]]* + $ + }x; + + min_by {length} grep {lc =~ /$target/} @str; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + is completing_word('aBc 11c' => 'accbbb', 'abc', 'abbc'), + 'accbbb', 'example 1'; + is completing_word('Da2 abc' => 'abcm', 'baacd', 'abaadc'), + 'baacd', 'example 2'; + is completing_word('JB 007' => 'jj', 'bb', 'bjb'), + 'bjb', 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
