aboutsummaryrefslogtreecommitdiff
path: root/challenge-243
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-11-13 10:31:57 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-11-17 15:36:34 +0100
commit85575a59583647688246d2d690bf66d3a14325cd (patch)
tree51d32055adca2b6a51d58ad506184e646896685c /challenge-243
parent1a59d9883fd5f113ea3b6ad4c045dcdc87fcdb83 (diff)
downloadperlweeklychallenge-club-85575a59583647688246d2d690bf66d3a14325cd.tar.gz
perlweeklychallenge-club-85575a59583647688246d2d690bf66d3a14325cd.tar.bz2
perlweeklychallenge-club-85575a59583647688246d2d690bf66d3a14325cd.zip
Solution to task 1
Diffstat (limited to 'challenge-243')
-rwxr-xr-xchallenge-243/jo-37/perl/ch-1.pl60
1 files changed, 60 insertions, 0 deletions
diff --git a/challenge-243/jo-37/perl/ch-1.pl b/challenge-243/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..0dc839cd04
--- /dev/null
+++ b/challenge-243/jo-37/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/perl -s
+
+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 integers
+
+EOS
+
+
+### Input and Output
+
+say count_reverse_pairs(@ARGV);
+
+
+### Implementation
+
+# Count element pairs where $j > $i and $nums[$i] > 2 * $nums[$j].
+
+sub count_reverse_pairs {
+ my $nums = long @_;
+
+ ((sequence($nums) > sequence(1, $nums->dim(0)))
+ & ($nums->dummy(0) > 2 * $nums))->sum;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is count_reverse_pairs(1, 3, 2, 3, 1), 2, 'example 1';
+ is count_reverse_pairs(2, 4, 3, 5, 1), 3, 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is count_reverse_pairs(1, 0, -1), 3, 'zero and negative';
+ }
+
+ done_testing;
+ exit;
+}