diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-04-19 16:15:38 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-04-19 16:15:38 +0200 |
| commit | d31dafebd4855cfdbc6453357ed9b0aec8a17bdb (patch) | |
| tree | 68198b4c737557ce146185b4795925a62b9cd7bc | |
| parent | be33dd2aba1140bd39954f64816a59e4b89e643c (diff) | |
| parent | 3c85a17ce532065ffff1f5a3ec3b1c9f10f334b7 (diff) | |
| download | perlweeklychallenge-club-d31dafebd4855cfdbc6453357ed9b0aec8a17bdb.tar.gz perlweeklychallenge-club-d31dafebd4855cfdbc6453357ed9b0aec8a17bdb.tar.bz2 perlweeklychallenge-club-d31dafebd4855cfdbc6453357ed9b0aec8a17bdb.zip | |
Solutions to challenge 265
| -rw-r--r-- | challenge-265/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-265/jo-37/perl/ch-1.pl | 60 | ||||
| -rwxr-xr-x | challenge-265/jo-37/perl/ch-2.pl | 72 |
3 files changed, 133 insertions, 0 deletions
diff --git a/challenge-265/jo-37/blog.txt b/challenge-265/jo-37/blog.txt new file mode 100644 index 0000000000..7d586274b8 --- /dev/null +++ b/challenge-265/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/04/19/ch-265.html diff --git a/challenge-265/jo-37/perl/ch-1.pl b/challenge-265/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..8f44c31fbb --- /dev/null +++ b/challenge-265/jo-37/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::Util 'reduce'; + +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 integers + +EOS + + +### Input and Output + +say appearance33(@ARGV); + + +### Implementation + +sub appearance33 { + my %count; + reduce { + ++$count{$b} * 100 / @_ >= 33 && $b < ($a // 'inf') ? $b : $a; + } undef, @_; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is appearance33(1,2,3,3,3,3,4,2), 3, 'example 1'; + is appearance33(1, 1), 1, 'example 2'; + is appearance33(1, 2, 3), 1, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + is appearance33((2, 3) x 33, (1) x 34), 1, 'exactly 33%'; + is appearance33((1, 2, 3) x 32, (4) x 4), U(), 'nothing found'; + } + + done_testing; + exit; +} 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; +} |
