aboutsummaryrefslogtreecommitdiff
path: root/challenge-197
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2022-12-30 16:16:31 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2022-12-30 16:22:56 +0100
commit52f0922230bfc9ef7949656cfae1cc52f234d636 (patch)
tree32c41b49afe721ee61336f8071ecab7bd57517c9 /challenge-197
parentef1b6e72f5f8b7b06df4f7f955522b0eb7f554fd (diff)
downloadperlweeklychallenge-club-52f0922230bfc9ef7949656cfae1cc52f234d636.tar.gz
perlweeklychallenge-club-52f0922230bfc9ef7949656cfae1cc52f234d636.tar.bz2
perlweeklychallenge-club-52f0922230bfc9ef7949656cfae1cc52f234d636.zip
Solution to task 1
Diffstat (limited to 'challenge-197')
-rwxr-xr-xchallenge-197/jo-37/perl/ch-1.pl59
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-197/jo-37/perl/ch-1.pl b/challenge-197/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..3a9bdb9c70
--- /dev/null
+++ b/challenge-197/jo-37/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+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 "(@{[move_zero(@ARGV)]})";
+
+
+### Implementation
+
+sub move_zero {
+ my $cnt;
+ # Count and suppress zeroes, then append the suppressed zeroes.
+ ((grep {$cnt += !$_; $_} @_), (0) x $cnt)
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [move_zero(1, 0, 3, 0, 0, 5)], [1, 3, 5, 0, 0, 0], 'example 1';
+ is [move_zero(1, 6, 4)], [1, 6, 4], 'example 2';
+ is [move_zero(0, 1, 0, 2, 0)], [1, 2, 0, 0, 0], 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is [move_zero(0, 0, 0)], [0, 0, 0], 'zeroes only';
+ is [move_zero(1, 0, -1)], [1, -1, 0], 'negative';
+ }
+
+ done_testing;
+ exit;
+}