aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-07-19 14:24:45 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-07-19 14:56:55 +0200
commit1c7b9d4602e1fe9f2f410265bd8a9229caeaffc1 (patch)
tree104908fdff41143922a670977e99e4e50c2890e6
parentecb93fa735b7c0895a3d5c5350828f3333ed503f (diff)
downloadperlweeklychallenge-club-1c7b9d4602e1fe9f2f410265bd8a9229caeaffc1.tar.gz
perlweeklychallenge-club-1c7b9d4602e1fe9f2f410265bd8a9229caeaffc1.tar.bz2
perlweeklychallenge-club-1c7b9d4602e1fe9f2f410265bd8a9229caeaffc1.zip
Solution to task 2
-rwxr-xr-xchallenge-278/jo-37/perl/ch-2.pl67
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-278/jo-37/perl/ch-2.pl b/challenge-278/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..5a25e71b63
--- /dev/null
+++ b/challenge-278/jo-37/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 2;
+usage: $0 [-examples] [-tests] [STR CHR]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ enable trace output
+
+STR
+ a string
+
+CHR
+ a character (or substring)
+
+EOS
+
+
+### Input and Output
+
+say reverse_word(@ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/07/19/ch-278.html#task-2
+
+
+sub reverse_word ($str, $char) {
+
+ $str =~ s#(.*?\Q$char\E)#join '', sort split //, $1#er;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ is reverse_word("challenge", "e"), "acehllnge", "example 1";
+ is reverse_word("programming", "a"), "agoprrmming", "example 2";
+ is reverse_word("champion", "b"), "champion", "example 3";
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is reverse_word("keep", "k"), "keep", "match first";
+ }
+
+ done_testing;
+ exit;
+}