diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-10-17 18:20:07 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-10-20 17:37:11 +0200 |
| commit | 197ba7aaec087645b790e50b93b6e6d66342b6f0 (patch) | |
| tree | e4846741251f612fd833a96f57e7fb24d591bf20 /challenge-239 | |
| parent | 69c00b031a8479741ce8c9cc547dca5429debab8 (diff) | |
| download | perlweeklychallenge-club-197ba7aaec087645b790e50b93b6e6d66342b6f0.tar.gz perlweeklychallenge-club-197ba7aaec087645b790e50b93b6e6d66342b6f0.tar.bz2 perlweeklychallenge-club-197ba7aaec087645b790e50b93b6e6d66342b6f0.zip | |
Solution to task 2
Diffstat (limited to 'challenge-239')
| -rwxr-xr-x | challenge-239/jo-37/perl/ch-2.pl | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/challenge-239/jo-37/perl/ch-2.pl b/challenge-239/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..22d7c4a317 --- /dev/null +++ b/challenge-239/jo-37/perl/ch-2.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples, $verbose, $allowed); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless $allowed && @ARGV; +usage: $0 [-examples] [-tests] [-verbose] [-allowed=A STR...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-verbose + print consistent strings instead of counting them + +-allowed=A + string of allowed characters + +STR... + list of strings + +EOS + + +### Input and Output + +say $verbose ? + "@{[count_consistent($allowed, @ARGV)]}" : + scalar count_consistent($allowed, @ARGV); + + +### Implementation + +sub count_consistent { + # there is no restriction to a character class in the task. So + # allow meta characters, too: + my $allowed = quotemeta shift; + grep /^[$allowed]*$/, @_; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is scalar count_consistent(ab => + qw(ad bd aaab baa badab)), 2, 'example 1'; + + is scalar count_consistent(abc => + qw(a b c ab ac bc abc)), 7, 'example 2'; + + is scalar count_consistent(cad => + qw(cc acd b ba bac bad ac d)), 4, 'example 3'; + + } + + SKIP: { + skip "tests" unless $tests; + + is [count_consistent('a-z].*[0-9' => + qw(bc+1 .]0a a*z b-y))], [qw(.]0a a*z)], 'meta chars'; + + is count_consistent(abc => ''), 1, 'allow empty'; + } + + done_testing; + exit; +} |
