diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-06-25 23:00:59 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-06-27 11:02:00 +0200 |
| commit | 64c5c668bc7fc65a660a27567b242606d29ae55f (patch) | |
| tree | fd6b4d496ee69c4a5115c1e0bb7794c319b79bd5 | |
| parent | 4b33a6c5bea8a2c5df93b32bac9078b4d23ff42f (diff) | |
| download | perlweeklychallenge-club-64c5c668bc7fc65a660a27567b242606d29ae55f.tar.gz perlweeklychallenge-club-64c5c668bc7fc65a660a27567b242606d29ae55f.tar.bz2 perlweeklychallenge-club-64c5c668bc7fc65a660a27567b242606d29ae55f.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-327/jo-37/perl/ch-1.pl | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/challenge-327/jo-37/perl/ch-1.pl b/challenge-327/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..96d5f55c81 --- /dev/null +++ b/challenge-327/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'; + + +### 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 - Task Title + + usage: $0 [-examples] [-tests] [N...] + + -examples + run the examples from the challenge + + -tests + run some tests + + N... + list of integers + + EOS +} + + +### Input and Output + +say "(@{[missing_ints(@ARGV)]})"; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/06/27/ch-327.html#task-1 + +sub missing_ints { + (\my %ints)->@{1 .. @_} = (); + delete @ints{@_}; + sort {$a <=> $b} keys %ints; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my @result = missing_ints(@$args); + is \@result, $expected, + "$name: (@$args) -> (@$expected)"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[1, 2, 1, 3, 2, 5], [4, 6], 'example 1'], + [[1, 1, 1], [2, 3], 'example 2'], + [[2, 2, 1], [3], 'example 3'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 2; + is [missing_ints(1 .. 10)], [], 'none missing'; + is [missing_ints((0) x 10)], [1 .. 10], 'all missing'; + }) : pass 'skip tests'; + + exit; +} |
