aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-09-23 18:33:31 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-09-23 22:31:51 +0100
commit0bea28dd4ed6f0bb255a1abc32a5144ba12bd33c (patch)
tree810526fdfc5793eb797c30166134153bc71cfd1c
parent2530d414ea81a4dc70bde6176653fffbd3c9d720 (diff)
downloadperlweeklychallenge-club-0bea28dd4ed6f0bb255a1abc32a5144ba12bd33c.tar.gz
perlweeklychallenge-club-0bea28dd4ed6f0bb255a1abc32a5144ba12bd33c.tar.bz2
perlweeklychallenge-club-0bea28dd4ed6f0bb255a1abc32a5144ba12bd33c.zip
Solution to task 2
-rwxr-xr-xchallenge-235/jo-37/perl/ch-2.pl63
1 files changed, 63 insertions, 0 deletions
diff --git a/challenge-235/jo-37/perl/ch-2.pl b/challenge-235/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..6075cac684
--- /dev/null
+++ b/challenge-235/jo-37/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use PDL;
+
+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 "(@{dup_zeroes(@ARGV)})";
+
+
+### Implementation
+
+# There's almost no need to comment this implementation as it literally
+# follows the task: Count sequential occurrences of every number, double
+# all zeroes' occurrences and trim the result to the original size.
+
+sub dup_zeroes {
+ my $n = long @_;
+ my ($l, $v) = rle $n;
+ $l->where($v == 0) *= 2;
+ rld($l, $v)->reshape($n->dims)->unpdl;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is dup_zeroes(1, 0, 2, 3, 0, 4, 5, 0),
+ [1, 0, 0, 2, 3, 0, 0, 4], 'example 1';
+ is dup_zeroes(1, 2, 3), [1, 2, 3], 'example 2';
+ is dup_zeroes(0, 3, 0, 4, 5), [0, 0, 3, 0, 0], 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}