aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-10-13 22:05:16 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-10-17 08:46:16 +0200
commit39ca03b80b7079658b5975c4905120cb801eb035 (patch)
tree0df1d4324d0e365c6bd0962ebc9b4e1eaf10640d
parent8ba1ac24dd58713d30f9b892735710546e87e560 (diff)
downloadperlweeklychallenge-club-39ca03b80b7079658b5975c4905120cb801eb035.tar.gz
perlweeklychallenge-club-39ca03b80b7079658b5975c4905120cb801eb035.tar.bz2
perlweeklychallenge-club-39ca03b80b7079658b5975c4905120cb801eb035.zip
Solution to task 2
-rwxr-xr-xchallenge-341/jo-37/perl/ch-2.pl103
1 files changed, 103 insertions, 0 deletions
diff --git a/challenge-341/jo-37/perl/ch-2.pl b/challenge-341/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..1c9dfee91e
--- /dev/null
+++ b/challenge-341/jo-37/perl/ch-2.pl
@@ -0,0 +1,103 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 -no_srand;
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+
+### Options and Arguments
+
+my ($tests, $examples, $verbose, $chr);
+GetOptions(
+ 'examples!' => \$examples,
+ 'tests!' => \$tests,
+ 'verbose!' => \$verbose,
+ 'chr=s' => \$chr,
+) or usage();
+
+$chr //= '';
+
+run_tests($examples, $tests); # tests do not return
+
+usage() unless @ARGV == 1;
+
+sub usage {
+ die <<~EOS;
+ $0 - Task Title
+
+ usage: $0 [-examples] [-tests] [-chr C STR]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ -char C
+ a character
+
+ STR
+ a string
+
+ EOS
+}
+
+
+### Input and Output
+
+say reverse_prefix($chr, shift);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/10/17/ch-341.html#task-2
+
+sub reverse_prefix ($chr, $str) {
+ $str =~ s/.*?$chr/reverse $&/er;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = reverse_prefix(@$args);
+ is $result, $expected,
+ "$name: (@$args) -> $expected";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [[qw(g programming)], 'gorpramming', 'example 1'],
+ [[qw(h hello)], 'hello', 'example 2'],
+ [[qw(h abcdefghij)], 'hgfedcbaij', 'example 3'],
+ [[qw(s reverse)], 'srevere', 'example 4'],
+ [[qw(r perl)], 'repl', 'example 5'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my @tests = (
+ [[qw(d abc)], 'abc', 'not found'],
+ [[qw(de abcdefg)], 'edcbafg', 'search string'],
+ [['', 'abc'], 'abc', 'no char'],
+ );
+ plan scalar @tests;
+ for (@tests) {
+ run_example @$_;
+ }
+ }) : pass 'skip tests';
+
+ exit;
+}