diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-08-02 21:38:35 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-08-02 21:38:35 +0200 |
| commit | fe58c36392d0ccbda93b896a2625c658ba3b7470 (patch) | |
| tree | 9a3965107dfb7ad770d7e7b1476ca5f815131662 | |
| parent | db9338fc6ae939315564be0822ac446f45471124 (diff) | |
| download | perlweeklychallenge-club-fe58c36392d0ccbda93b896a2625c658ba3b7470.tar.gz perlweeklychallenge-club-fe58c36392d0ccbda93b896a2625c658ba3b7470.tar.bz2 perlweeklychallenge-club-fe58c36392d0ccbda93b896a2625c658ba3b7470.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-228/jo-37/perl/ch-2.pl | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-228/jo-37/perl/ch-2.pl b/challenge-228/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..ce0d921515 --- /dev/null +++ b/challenge-228/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 numbers + +EOS + + +### Input and Output + +say zero_array(@ARGV); + + +### Implementation + +sub zero_array { + my @sort = sort {$a <=> $b} @_; + my ($head, $count); + while (@_) { + ($head = shift) == $sort[0] ? shift @sort : push @_, $head; + $count++; + } + $count; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is zero_array(3, 4, 2), 5, 'example 1'; + is zero_array(1, 2, 3), 3, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
