diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-07-18 14:35:04 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-07-21 12:44:52 +0200 |
| commit | 03d66b78fd33f9484d2fd161f2130b69fffd4d0d (patch) | |
| tree | 60f2d100d15573991bf344d7d9566f6a6bd737be | |
| parent | 23e8a20883c91a0950db37c8ffa95d775bdef4c0 (diff) | |
| download | perlweeklychallenge-club-03d66b78fd33f9484d2fd161f2130b69fffd4d0d.tar.gz perlweeklychallenge-club-03d66b78fd33f9484d2fd161f2130b69fffd4d0d.tar.bz2 perlweeklychallenge-club-03d66b78fd33f9484d2fd161f2130b69fffd4d0d.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-226/jo-37/perl/ch-2.pl | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-226/jo-37/perl/ch-2.pl b/challenge-226/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..8a621bd7ff --- /dev/null +++ b/challenge-226/jo-37/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +N... + list of non-negative numbers + +EOS + + +### Input and Output + +say zero_array(@ARGV); + + +### Implementation + +# To turn all elements to zero following the given rules, we need as +# many steps as there are distinct non-zero elements in the list. +sub zero_array { + (\my %v)->@{@_} = (); + scalar grep $_, keys %v; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is zero_array(1, 5, 0, 3, 5), 3, 'example 1'; + is zero_array(0), 0, 'example 2'; + is zero_array(2, 1, 4, 0, 3), 4, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is zero_array(1, 2, 1, 2), 2, 'no zero'; + } + + done_testing; + exit; +} |
