diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-05-02 22:22:42 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-05-02 22:22:42 +0200 |
| commit | 6b8a5c8681466f20dd6579f0a6eb02fbbe15dc91 (patch) | |
| tree | 17fd26163637dd3f66b089b511e69e5839786b4a | |
| parent | 6c6480baed8abb849ed2d2ffa29f5e8710b64268 (diff) | |
| parent | 5f4c85c782caa76b38e9e394a404d5ae2b611ee8 (diff) | |
| download | perlweeklychallenge-club-6b8a5c8681466f20dd6579f0a6eb02fbbe15dc91.tar.gz perlweeklychallenge-club-6b8a5c8681466f20dd6579f0a6eb02fbbe15dc91.tar.bz2 perlweeklychallenge-club-6b8a5c8681466f20dd6579f0a6eb02fbbe15dc91.zip | |
Challenge 184
| -rwxr-xr-x | challenge-184/jo-37/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-184/jo-37/perl/ch-2.pl | 65 |
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; +} |
