diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-02-06 18:04:46 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-02-09 18:11:09 +0100 |
| commit | 8e4fffb86e745d51f7b5de19f817330ac7444b71 (patch) | |
| tree | 5553db59667d21ef88db6001f8d152dbd4aa2607 | |
| parent | 1fe5253aebbcb4e8c92635f9c6fbb7218b439ac6 (diff) | |
| download | perlweeklychallenge-club-8e4fffb86e745d51f7b5de19f817330ac7444b71.tar.gz perlweeklychallenge-club-8e4fffb86e745d51f7b5de19f817330ac7444b71.tar.bz2 perlweeklychallenge-club-8e4fffb86e745d51f7b5de19f817330ac7444b71.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-255/jo-37/perl/ch-2.pl | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-255/jo-37/perl/ch-2.pl b/challenge-255/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..34336aeabf --- /dev/null +++ b/challenge-255/jo-37/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::UtilsBy 'max_by'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV > 1; +usage: $0 [-examples] [-tests] [P W...] + +-examples + run the examples from the challenge + +-tests + run some tests + +P + a paragraph + +W... + list of banned words + +EOS + + +### Input and Output + +say mfw(@ARGV); + + +### Implementation + +sub mfw { + my $p = shift; + my %count; + $count{$_}++ for split /\W+/, $p; + delete @count{@_}; + max_by {$count{$_}} keys %count; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is mfw('Joe hit a ball, the hit ball flew far after it was hit.', + 'hit'), 'ball', 'example 1'; + is mfw('Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.', + 'the'), 'Perl', 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is mfw('one two three four five two three four five three four five four five five', 'two', 'four', 'five'), 'three', 'more banned words'; + } + + done_testing; + exit; +} |
