diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-02-09 18:11:34 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-02-09 18:11:34 +0100 |
| commit | 9b06f505f1b1c2751c661069e4d167b78145d91e (patch) | |
| tree | 884c2547292b5752d0d9d9a604db18d0d2156370 | |
| parent | 399287fcd1fa2800c9ab44b9a065116e9a55ec86 (diff) | |
| parent | 19da2726efeaba8c0f314a15a41ddbdfcce4be64 (diff) | |
| download | perlweeklychallenge-club-9b06f505f1b1c2751c661069e4d167b78145d91e.tar.gz perlweeklychallenge-club-9b06f505f1b1c2751c661069e4d167b78145d91e.tar.bz2 perlweeklychallenge-club-9b06f505f1b1c2751c661069e4d167b78145d91e.zip | |
Solutions to challenge 255
| -rw-r--r-- | challenge-255/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-255/jo-37/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-255/jo-37/perl/ch-2.pl | 65 |
3 files changed, 127 insertions, 0 deletions
diff --git a/challenge-255/jo-37/blog.txt b/challenge-255/jo-37/blog.txt new file mode 100644 index 0000000000..cf2fd5b28e --- /dev/null +++ b/challenge-255/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/blog/pwc/challenge-255 diff --git a/challenge-255/jo-37/perl/ch-1.pl b/challenge-255/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..1ecc01addb --- /dev/null +++ b/challenge-255/jo-37/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [-tests] [S T] + +-examples + run the examples from the challenge + +-tests + run some tests + +S T + two strings + +EOS + + +### Input and Output + +say "@{[char_diff(@ARGV)]}"; + + +### Implementation + +sub char_diff ($s, $t) { + my %count; + $count{$_}++ for split //, $t; + $count{$_}-- for split //, $s; + no warnings 'numeric'; + map +($_) x $count{$_}, keys %count; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is [char_diff('Perl', 'Preel')], ['e'], 'example 1'; + is [char_diff('Weekly', 'Weeakly')], ['a'], 'example 2'; + is [char_diff('Box', 'Boxy')], ['y'], 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is [char_diff('abcde', 'ddd')], ['d', 'd'], 'more than one'; + } + + done_testing; + exit; +} 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; +} |
