aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-17 18:28:42 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-23 18:03:42 +0100
commit523063856f46735c0f0ea477b9cf73a7aa450807 (patch)
treeab3a94131ef5ba3b84e876e50349521af39a700a
parent47a94633bb256f4f2ba93f2e45f25d6b4a40cc09 (diff)
downloadperlweeklychallenge-club-523063856f46735c0f0ea477b9cf73a7aa450807.tar.gz
perlweeklychallenge-club-523063856f46735c0f0ea477b9cf73a7aa450807.tar.bz2
perlweeklychallenge-club-523063856f46735c0f0ea477b9cf73a7aa450807.zip
Challenge 023 task 1
-rwxr-xr-xchallenge-023/jo-37/perl/ch-1.pl64
1 files changed, 64 insertions, 0 deletions
diff --git a/challenge-023/jo-37/perl/ch-1.pl b/challenge-023/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..f57add51e9
--- /dev/null
+++ b/challenge-023/jo-37/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0 '!float';
+use PDL;
+use PDL::NiceSlice;
+
+our ($tests, $examples, $order);
+$order //= 1;
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [-order=N] [K...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-order=N
+ find forward difference sequence of order N. Default: 1
+
+K...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say "@{diff($order, @ARGV)}";
+
+
+### Implementation
+
+sub diff {
+ my $n = shift;
+ my $l = pdl @_;
+ $l = $l(1:-1) - $l(0:-2) for 1 .. $n;
+ $l->unpdl;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is diff(1, [5, 9, 2, 8, 1, 6]),
+ [4, -7, 6, -7, 5], 'example 1';
+ is diff(2, [5, 9, 2, 8, 1, 6]),
+ [-11, 13, -13, 12], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}