aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-05-02 19:34:11 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-05-02 22:21:28 +0200
commit5f4c85c782caa76b38e9e394a404d5ae2b611ee8 (patch)
tree17fd26163637dd3f66b089b511e69e5839786b4a
parent94a106458b8f2b30131f0b7dafe301b96c28ff81 (diff)
downloadperlweeklychallenge-club-5f4c85c782caa76b38e9e394a404d5ae2b611ee8.tar.gz
perlweeklychallenge-club-5f4c85c782caa76b38e9e394a404d5ae2b611ee8.tar.bz2
perlweeklychallenge-club-5f4c85c782caa76b38e9e394a404d5ae2b611ee8.zip
Challenge 184 task 2
-rwxr-xr-xchallenge-184/jo-37/perl/ch-2.pl65
1 files changed, 65 insertions, 0 deletions
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;
+}