diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-07-07 22:43:34 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-07-11 22:58:11 +0200 |
| commit | 063a0771670593aa3c1305704d02145277384878 (patch) | |
| tree | 30e73d4f0cc16d7854c2da98e757645563d862b3 /challenge-329 | |
| parent | 416e96fc2b3e5ef01a2fd7bc3107b19f88f1f767 (diff) | |
| download | perlweeklychallenge-club-063a0771670593aa3c1305704d02145277384878.tar.gz perlweeklychallenge-club-063a0771670593aa3c1305704d02145277384878.tar.bz2 perlweeklychallenge-club-063a0771670593aa3c1305704d02145277384878.zip | |
Solution to task 1
Diffstat (limited to 'challenge-329')
| -rwxr-xr-x | challenge-329/jo-37/perl/ch-1.pl | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/challenge-329/jo-37/perl/ch-1.pl b/challenge-329/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..5808ec1220 --- /dev/null +++ b/challenge-329/jo-37/perl/ch-1.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 -no_srand; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use List::Util 'uniqint'; + + +### 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 == 1; + +sub usage { + die <<~EOS; + $0 - count integers + + usage: $0 [-examples] [-tests] [STR] + + -examples + run the examples from the challenge + + -tests + run some tests + + STR + a string + + EOS +} + + +### Input and Output + +say "@{[count_int(shift)]}"; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/07/11/ch-329.html#task-1 + + +sub count_int { + uniqint shift =~ /\d+/g; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my @result = count_int(@$args); + is \@result, $expected, + "$name: (@$args) -> (@$expected)"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["the1weekly2challenge2"], [1, 2], 'example 1'], + [["go21od1lu5c7k"], [21, 1, 5, 7], 'example 2'], + [["4p3e2r1l"], [4, 3, 2, 1], 'example 3'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 1; + pass 'no tests'; + }) : pass 'skip tests'; + + exit; +} |
