aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-19 16:03:26 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-19 16:03:26 +0100
commit2084950ba8bca108d65373ba669cd71a147b722e (patch)
treee877efc5fd956b633fc4eade2d560646ad9a6960
parent8ae3d3895abc47167d4e768a2efe60616d25db60 (diff)
parentae820752c311eb4e28783fe59253f5fadd4cd0ad (diff)
downloadperlweeklychallenge-club-2084950ba8bca108d65373ba669cd71a147b722e.tar.gz
perlweeklychallenge-club-2084950ba8bca108d65373ba669cd71a147b722e.tar.bz2
perlweeklychallenge-club-2084950ba8bca108d65373ba669cd71a147b722e.zip
Solutions to task 252
-rw-r--r--challenge-252/jo-37/blog.txt1
-rwxr-xr-xchallenge-252/jo-37/perl/ch-1.pl58
-rwxr-xr-xchallenge-252/jo-37/perl/ch-2.pl67
3 files changed, 126 insertions, 0 deletions
diff --git a/challenge-252/jo-37/blog.txt b/challenge-252/jo-37/blog.txt
new file mode 100644
index 0000000000..c7539cbf2b
--- /dev/null
+++ b/challenge-252/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/blog/pwc/challenge-252
diff --git a/challenge-252/jo-37/perl/ch-1.pl b/challenge-252/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..0b1cc1f5c2
--- /dev/null
+++ b/challenge-252/jo-37/perl/ch-1.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use PDL;
+use PDL::NiceSlice;
+use Math::Prime::Util 'divisors';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [I...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+I...
+ list of integers
+
+EOS
+
+
+### Input and Output
+
+say snss(@ARGV);
+
+
+### Implementation
+
+sub snss {
+ sum pdl(@_)->(indx(divisors @_) - 1) ** 2
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is snss(1, 2, 3, 4), 21, 'example 1';
+ is snss(2, 7, 1, 19, 18, 3), 63, 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is snss(-1, -2), 5, 'negative values';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-252/jo-37/perl/ch-2.pl b/challenge-252/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..7269103def
--- /dev/null
+++ b/challenge-252/jo-37/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl -s
+
+use v5.26;
+use Test2::V0;
+use experimental 'signatures';
+
+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
+ positive integer
+
+EOS
+
+
+### Input and Output
+
+say "(@{[uniq_sum_zero(shift)]})";
+
+
+### Implementation
+
+sub uniq_sum_zero ($n) {
+ (-$n / 2 .. -1, (0) x ($n % 2), 1 .. $n / 2);
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ use List::Util qw(uniq sum);
+
+ my sub test_usz {
+ plan 2;
+ is scalar(uniq @_), scalar @_, 'uniq';
+ is sum(@_), 0, 'sum zero';
+ }
+
+ SKIP: {
+ skip "examples" unless $examples;
+
+ subtest 'example 1', \&test_usz, uniq_sum_zero(5);
+ subtest 'example 2', \&test_usz, uniq_sum_zero(3);
+ subtest 'example 3', \&test_usz, uniq_sum_zero(1);
+
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ subtest 'test 2', \&test_usz, uniq_sum_zero(2);
+ subtest 'test 4', \&test_usz, uniq_sum_zero(4);
+ }
+
+ done_testing;
+ exit;
+}