aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-05-02 19:33:58 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-05-02 22:21:28 +0200
commit94a106458b8f2b30131f0b7dafe301b96c28ff81 (patch)
tree8edf2e21a1ed5fab5a5f39414028c069bedc6efe
parent6c6480baed8abb849ed2d2ffa29f5e8710b64268 (diff)
downloadperlweeklychallenge-club-94a106458b8f2b30131f0b7dafe301b96c28ff81.tar.gz
perlweeklychallenge-club-94a106458b8f2b30131f0b7dafe301b96c28ff81.tar.bz2
perlweeklychallenge-club-94a106458b8f2b30131f0b7dafe301b96c28ff81.zip
Challenge 184 task 1
-rwxr-xr-xchallenge-184/jo-37/perl/ch-1.pl64
1 files changed, 64 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;
+}