diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-02-07 11:00:27 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-02-07 11:06:54 +0100 |
| commit | 36f974a8975b6c018276d08fd89e645ca215933a (patch) | |
| tree | 23eda9af1c004c03ab51aaa790106da61911afe8 | |
| parent | 07ce8b170ad1b8e91d653c9d7dd64c7f3dc210d2 (diff) | |
| download | perlweeklychallenge-club-36f974a8975b6c018276d08fd89e645ca215933a.tar.gz perlweeklychallenge-club-36f974a8975b6c018276d08fd89e645ca215933a.tar.bz2 perlweeklychallenge-club-36f974a8975b6c018276d08fd89e645ca215933a.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-307/jo-37/perl/ch-2.pl | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/challenge-307/jo-37/perl/ch-2.pl b/challenge-307/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..8826338789 --- /dev/null +++ b/challenge-307/jo-37/perl/ch-2.pl @@ -0,0 +1,95 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 '!float'; +use Test2::Tools::Subtest 'subtest_streamed'; +use Test2::Tools::PDL; +use Getopt::Long; +use experimental 'signatures'; +use utf8; + +use PDL; +use PDL::Char; + + +### Options and Arguments + +my ($tests, $examples, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV; + +sub usage { + die <<~EOS; + $0 - Find anagrams + + usage: $0 [-examples] [-tests] [W...] + + -examples + run the examples from the challenge + + -tests + run some tests + + W... + list of words + + EOS +} + + +### Input and Output + +say count_anagrams(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/02/07/ch-307.html#task-2 + +sub count_anagrams { + 1 + PDL::Char->new(map fc, @_)->qsort->enumvecg->at(-1); +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = count_anagrams(@$args); + is $result, $expected, + "$name: (@$args) -> $expected"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["acca", "dog", "god", "perl", "repl"], 3, 'example 1'], + [["abba", "baba", "aabb", "ab", "ab"], 2, 'example 2'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 3; + is count_anagrams(qw(ab bc ba)), 3, 'non-adjacent'; + is count_anagrams(qw(Ruffle Flufre)), 1, + 'fold case, ligature ffl'; + is count_anagrams(qw(ab bc cd dc)), 3, 'last group'; + }) : pass 'skip tests'; + + exit; +} |
