aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-02 23:06:47 +0100
committerGitHub <noreply@github.com>2023-05-02 23:06:47 +0100
commitc2bcc8ac11ed87ca49a85282480c8c20b7cf323a (patch)
tree17fd26163637dd3f66b089b511e69e5839786b4a
parent6c6480baed8abb849ed2d2ffa29f5e8710b64268 (diff)
parent6b8a5c8681466f20dd6579f0a6eb02fbbe15dc91 (diff)
downloadperlweeklychallenge-club-c2bcc8ac11ed87ca49a85282480c8c20b7cf323a.tar.gz
perlweeklychallenge-club-c2bcc8ac11ed87ca49a85282480c8c20b7cf323a.tar.bz2
perlweeklychallenge-club-c2bcc8ac11ed87ca49a85282480c8c20b7cf323a.zip
Merge pull request #8010 from jo-37/subsequent
Solutions to challenge 184
-rwxr-xr-xchallenge-184/jo-37/perl/ch-1.pl64
-rwxr-xr-xchallenge-184/jo-37/perl/ch-2.pl65
2 files changed, 129 insertions, 0 deletions
diff --git a/challenge-184/jo-37/perl/ch-1.pl b/challenge-184/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..5e34086741
--- /dev/null
+++ b/challenge-184/jo-37/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [STRING...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+STRING
+ list of strings
+
+EOS
+
+
+### Input and Output
+
+say "(@{[sequence_number(@ARGV)]})";
+
+
+### Implementation
+
+# It is not clear how to maintain prefixes that come out of order. In
+# the examples the prefixes are simply replaced with a two-digit number.
+sub sequence_number {
+ my $seq = 0;
+ map s/^[a-z]{2}(?=\d+$)/sprintf "%02d", $seq++/er, @_;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [sequence_number('ab1234', 'cd5678', 'ef1342')],
+ ['001234', '015678', '021342'], 'example 1';
+
+ is [sequence_number('pq1122', 'rs3334')],
+ ['001122', '013334'], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is [sequence_number('112233')], ['112233'], 'no prefix';
+ is [sequence_number('abc123')], ['abc123'], 'prefix too long';
+ is [sequence_number('ab')], ['ab'], 'no suffix';
+
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-184/jo-37/perl/ch-2.pl b/challenge-184/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..8927c04ab5
--- /dev/null
+++ b/challenge-184/jo-37/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use List::MoreUtils qw(part);
+use List::UtilsBy qw(zip_by);
+use Data::Dump;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [STRING...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+STRING...
+ list of strings, where each string consists of space-separated
+ digits and letters
+
+EOS
+
+
+### Input and Output
+
+dd split_array(@ARGV);
+
+
+### Implementation
+
+# Split each string into its space separated elements, distribute these
+# onto two buckets of non-digits and digits, collect alike buckets and
+# drop empty ones.
+sub split_array {
+ [map {[grep $_, @$_]}
+ zip_by {[@_]}
+ map {[part {/\D/} @$_]}
+ map [split], @_];
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is split_array('a 1 2 b 0', '3 c 4 d'),
+ [[[1,2,0], [3,4]], [['a','b'], ['c','d']]], 'example 1';
+ is split_array('1 2', 'p q r', 's 3', '4 5 t'),
+ [[[1,2], [3], [4,5]], [['p','q','r'], ['s'], ['t']]], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}