aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-258/jo-37/blog.txt1
-rwxr-xr-xchallenge-258/jo-37/perl/ch-1.pl62
-rwxr-xr-xchallenge-258/jo-37/perl/ch-2.pl60
3 files changed, 123 insertions, 0 deletions
diff --git a/challenge-258/jo-37/blog.txt b/challenge-258/jo-37/blog.txt
new file mode 100644
index 0000000000..f757f71bbc
--- /dev/null
+++ b/challenge-258/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2024/03/01/ch-258.html
diff --git a/challenge-258/jo-37/perl/ch-1.pl b/challenge-258/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..510091a56d
--- /dev/null
+++ b/challenge-258/jo-37/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use Math::Prime::Util 'todigits';
+
+our ($tests, $examples, $base);
+$base ||= 10;
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [-base=BASE] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ enable trace output
+
+-base=BASE
+ regard numbers in base BASE. Default: 10
+
+N...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say cedn($base, @ARGV);
+
+
+### Implementation
+
+sub cedn {
+ my $base = shift;
+ scalar grep !(scalar(todigits($_, $base)) % 2), @_
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is cedn(10 => 10, 1, 111, 24, 1000), 3, 'example 1';
+ is cedn(10 => 111, 1, 11111), 0, 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-258/jo-37/perl/ch-2.pl b/challenge-258/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..c9bf4e62d7
--- /dev/null
+++ b/challenge-258/jo-37/perl/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use List::Util 'sum0';
+
+our ($tests, $examples, $bits);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless defined $bits and @ARGV;
+usage: $0 [-examples] [-tests] [-bits=K] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-bits=K
+ sum over numbers with a k-bit index
+
+N...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say k_bit_sum($bits, @ARGV);
+
+
+### Implementation
+
+sub k_bit_sum {
+ my $k = shift;
+
+ sum0 @_[grep unpack('%32b*', pack('l', $_)) == $k, 0 .. $#_];
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is k_bit_sum(1 ,=> 2, 5, 9, 11, 3), 17, 'example 1';
+ is k_bit_sum(2 ,=> 2, 5, 9, 11, 3), 11, 'example 2';
+ is k_bit_sum(0 ,=> 2, 5, 9, 11, 3), 2, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}