aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-23 16:19:23 +0100
committerGitHub <noreply@github.com>2025-10-23 16:19:23 +0100
commitd3e6dbfcd4f23330c9fc0e39d106960c8436f91c (patch)
tree9855dd9d314715e9d2cbc0284d3d217e992978a1
parent7deccc245b98de87942adf5ae3745dc9c783e6ed (diff)
parentf9875fc40b511a631b24e41459e244319fb27a20 (diff)
downloadperlweeklychallenge-club-d3e6dbfcd4f23330c9fc0e39d106960c8436f91c.tar.gz
perlweeklychallenge-club-d3e6dbfcd4f23330c9fc0e39d106960c8436f91c.tar.bz2
perlweeklychallenge-club-d3e6dbfcd4f23330c9fc0e39d106960c8436f91c.zip
Merge pull request #12907 from jo-37/contrib
Solutions to challenge 344
-rw-r--r--challenge-344/jo-37/blog.txt1
-rwxr-xr-xchallenge-344/jo-37/perl/ch-1.pl101
-rwxr-xr-xchallenge-344/jo-37/perl/ch-2.pl143
3 files changed, 245 insertions, 0 deletions
diff --git a/challenge-344/jo-37/blog.txt b/challenge-344/jo-37/blog.txt
new file mode 100644
index 0000000000..f8f7a7dc90
--- /dev/null
+++ b/challenge-344/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2025/10/23/ch-344.html
diff --git a/challenge-344/jo-37/perl/ch-1.pl b/challenge-344/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..22ae8a2a60
--- /dev/null
+++ b/challenge-344/jo-37/perl/ch-1.pl
@@ -0,0 +1,101 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 -no_srand;
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+use Math::Prime::Util qw(fromdigits todigits);
+
+
+### Options and Arguments
+
+my ($tests, $examples, $verbose, $x);
+GetOptions(
+ 'examples!' => \$examples,
+ 'tests!' => \$tests,
+ 'verbose!' => \$verbose,
+ 'x=i' => \$x,
+) or usage();
+
+run_tests($examples, $tests); # tests do not return
+
+usage() unless @ARGV && defined $x;
+
+sub usage {
+ die <<~EOS;
+ $0 - Task Title
+
+ usage: $0 [-examples] [-tests] [-verbose] [--] [-x X N...]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ -x
+ an integer
+
+ N...
+ list of integers
+
+ EOS
+}
+
+
+### Input and Output
+
+say "(@{[array_form_comp($x, \@ARGV)]})";
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/10/23/ch-344.html#task-1
+
+sub array_form_comp ($x, $ints) {
+ todigits fromdigits($ints) + $x;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = [array_form_comp(@$args)];
+ like $result, $expected,
+ "$name: ($args->[1]->@*) + $args->[0] -> (@$expected)";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [[12, [1, 2, 3, 4]], [1, 2, 4, 6], 'example 1'],
+ [[181, [2, 7, 4]], [4, 5, 5], 'example 2'],
+ [[1, [9, 9, 9]], [1, 0, 0, 0], 'example 3'],
+ [[999, [1, 0, 0, 0]], [1, 9, 9, 9], 'example 4'],
+ [[1000, [0]], [1, 0, 0, 0], 'example 5'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my @tests = (
+ [[-1, [1, 0, 0]], [9, 9], 'minus'],
+ );
+ plan scalar @tests;
+ for (@tests) {
+ run_example @$_;
+ }
+ }) : pass 'skip tests';
+
+ exit;
+}
diff --git a/challenge-344/jo-37/perl/ch-2.pl b/challenge-344/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..3acf6de8fa
--- /dev/null
+++ b/challenge-344/jo-37/perl/ch-2.pl
@@ -0,0 +1,143 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 -no_srand;
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+use Data::Dump;
+
+use List::MoreUtils 'frequency';
+
+
+### Options and Arguments
+
+my ($tests, $examples, $verbose, $target);
+GetOptions(
+ 'examples!' => \$examples,
+ 'tests!' => \$tests,
+ 'verbose!' => \$verbose,
+ 'target=s' => \$target,
+) or usage();
+
+run_tests($examples, $tests); # tests do not return
+
+usage() unless defined $target && @ARGV;
+
+sub usage {
+ die <<~EOS;
+ $0 - Task Title
+
+ usage: $0 [-examples] [-tests] [-verbose] [--] [-target T,... S...]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ -verbose
+ enable trace output
+
+ -target T,...
+ list of space and/or comma separated integers
+
+ S...
+ list of integer arrays, each consisting of space and/or comma
+ separated numbers
+
+ EOS
+}
+
+
+### Input and Output
+
+say +(qw(true false))[!array_formation([map [split /[\s,]\s*/], @ARGV], [split /[\s,]\s*/, $target])];
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/10/23/ch-344.html#task-2
+
+sub array_formation ($source, $target) {
+ array_formation_r(map join("\cG", @$_, ''), $target, @$source);
+}
+
+sub array_formation_r ($target, @source) {
+ !@source && return !$target;
+ my (@done, %seen);
+ while (defined (my $source = shift @source)) {
+ $seen{$source}++ && next;
+ my $match = (my $tail = $target) =~ s/^\Q$source//;
+ !@source && !@done && return $match && !$tail;
+ $match && array_formation_r($tail, @done, @source) && return 1;
+ } continue {
+ push @done, $source;
+ }
+ _:
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name, $reason=undef) {
+ {
+ my $todo;
+ $todo = todo $reason if $reason;
+ my $result = array_formation(@$args);
+ is $result, $expected,
+ "$name: (@{[map qq{[@$_]}, $args->[0]->@*]}, [$args->[1]->@*]) -> " . $expected->name;
+ }}
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [[[[2,3], [1], [4]], [1, 2, 3, 4]], T(), 'example 1'],
+ [[[[1,3], [2,4]], [1, 2, 3, 4]], F(), 'example 2'],
+ [[[[9,1], [5,8], [2]], [5, 8, 2, 9, 1]], T(), 'example 3'],
+ [[[[1], [3]], [1, 2, 3]], F(), 'example 4'],
+ [[[[4, 7, 6]], [4, 7, 6]], T(), 'example 5'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my $n = 10;
+ my @tests = (
+ [[[[1,2,3], [4, 5]], [1, 2, 3, 4, 5, 6]], F(), 'target too long'],
+ [[[], [1]], F(), 'empty source'],
+ [[[], []], T(), 'empty source and target'],
+ [[[[1,2,3], [3, 4], [1, 2]], [1, 2, 3, 3, 4, 1, 2]], T(),
+ 'pick first'],
+ [[[[1,2,3], [3, 4], [1, 2]], [1, 2, 3, 4, 1, 2, 3]], T(),
+ 'pick second'],
+ [[[[qw(ab cd)], [qw(ac bd)]], [qw(ac bd ab cd)]], T(),
+ 'match strings, true'],
+ [[[[qw(ab cd)], [qw(ac bd)]], [qw(ab ac cd bd)]], F(),
+ 'match strings, false'],
+ [[[[(".*") x 4]], [qw(ab ac cd bd)]], F(),
+ 'do not match meta chars'],
+ [[[[qw(ab cd)], [qw(ac bd)]], [qw(ab ac cd bd)]], F(),
+ 'match strings, false'],
+ [[[["a"], ["b"], ["c\cGd"]], ["a\cGb", "c", "d"]], F(),
+ 'abusing the separator', 'should fail'],
+ [[[([1,1]) x $n , [1, 2]], [(1, 1) x $n, 2, 1]], F(), 'fail faster'],
+ [[[([1,1]) x $n , [1, 2]], [(1, 1) x $n, 1, 2]], T(), 'cross check'],
+ [[[[2], [], [1]], [1, 2]], T(), 'empty source element'],
+ );
+ plan scalar @tests;
+ for (@tests) {
+ run_example @$_;
+ }
+ }) : pass 'skip tests';
+
+ exit;
+}