aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-02-06 18:31:21 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-02-10 15:25:20 +0100
commit58d82f030937dbe7424273922083292eba3550cf (patch)
tree74df805ac3d63c0cd75561a112944a198d09ad4a
parentf92e84261b474f81c014f4982268d6e2797b66d9 (diff)
downloadperlweeklychallenge-club-58d82f030937dbe7424273922083292eba3550cf.tar.gz
perlweeklychallenge-club-58d82f030937dbe7424273922083292eba3550cf.tar.bz2
perlweeklychallenge-club-58d82f030937dbe7424273922083292eba3550cf.zip
Solution to task 1
-rwxr-xr-xchallenge-203/jo-37/perl/ch-1.pl66
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-203/jo-37/perl/ch-1.pl b/challenge-203/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..b561301a8d
--- /dev/null
+++ b/challenge-203/jo-37/perl/ch-1.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/perl -s
+
+use v5.22;
+use Test2::V0;
+use Math::Prime::Util qw(forcomb vecsum);
+use experimental 'refaliasing';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV >= 4;
+usage: $0 [-examples] [-tests] [-verbose] [N1 N2 N3 N4...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N1 N2 N3 N4...
+ list of at least four numbers
+
+EOS
+
+
+### Input and Output
+
+say csq(@ARGV);
+
+
+### Implementation
+
+# Count Special Quadruplets:
+# Loop over k-combinations of n, pick the corresponding values, check
+# for the "special quadruplets" condition and count.
+sub csq {
+ \my @l = \@_;
+ my $cnt;
+ # Perl guarantees left-to-right evaluation of operator arguments, so
+ # "pop" is performed before the slice @l[@_] is taken.
+ forcomb {$cnt += $l[pop] == vecsum @l[@_]} @l, 4;
+ $cnt;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is csq(1,2,3,6), 1, 'example 1';
+ is csq(1,1,1,3,5), 4, 'example 2';
+ is csq(3,3,6,4,5), 0, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is csq(0, 1, 2, 3, 6, 11, 20, 37, 68, 125), 7, 'strict sums';
+ }
+
+ done_testing;
+ exit;
+}