aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-04 11:21:15 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-04 11:28:28 +0200
commit6c65435a654e667e1c153e12dc83b5a44b4ecc89 (patch)
treef3f4b08910b948740c0eebaa3d3a73fdeadf9171
parentb23df451821993e60fe094025fbfba4b29e61d06 (diff)
downloadperlweeklychallenge-club-6c65435a654e667e1c153e12dc83b5a44b4ecc89.tar.gz
perlweeklychallenge-club-6c65435a654e667e1c153e12dc83b5a44b4ecc89.tar.bz2
perlweeklychallenge-club-6c65435a654e667e1c153e12dc83b5a44b4ecc89.zip
Solution to task 2
-rwxr-xr-xchallenge-328/jo-37/perl/ch-2.pl110
1 files changed, 110 insertions, 0 deletions
diff --git a/challenge-328/jo-37/perl/ch-2.pl b/challenge-328/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..213d08af72
--- /dev/null
+++ b/challenge-328/jo-37/perl/ch-2.pl
@@ -0,0 +1,110 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 -no_srand;
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+use String::Compile::Tr;
+
+
+### Options and Arguments
+
+my ($tests, $examples, $remove_pairs, $make_good, $verbose);
+GetOptions(
+ 'examples!' => \$examples,
+ 'tests!' => \$tests,
+ 'verbose!' => \$verbose,
+ '1!' => \$remove_pairs,
+ '2!' => \$make_good,
+) or usage();
+$remove_pairs = 1 unless $make_good;
+
+run_tests($examples, $tests); # tests do not return
+
+usage() unless @ARGV == 1;
+
+sub usage {
+ die <<~EOS;
+ $0 - good string
+
+ usage: $0 [-examples] [-tests] [STR]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ STR
+ a string
+
+ EOS
+}
+
+
+### Input and Output
+
+say "remove pairs: '@{[remove_pairs(@ARGV)]}'" if $remove_pairs;
+say "make good: '@{[make_good(@ARGV)]}'" if $make_good;
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/07/04/ch-328.html#task-2
+
+
+sub remove_pairs ($str) {
+ 1 while $str =~ s/(\p{Lu})(??{lc $1})|(\p{Ll})(??{uc $2})//;
+ $str;
+}
+
+sub make_good {
+ shift =~ s/(\p{LC})\1+/
+ trgen(2 * trgen(lc($1))->($&) < length($&) ? lc($1) : uc($1),
+ '', 'dr')->($&);
+ /iegr;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($str, $removed, $good, $name) {
+ subtest(qq{$name: "$str"} => sub {
+ plan 2;
+ my $result1 = remove_pairs($str);
+ is $result1, $removed,
+ qq{remove pairs -> "$removed"};
+ my $result2 = make_good($str);
+ is $result2, $good,
+ qq{make good -> "$good"};
+ });
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ ['WeEeekly', 'Weekly', 'Weeekly', 'example 1'],
+ ['abBAdD', '', 'abAd', 'example 2'],
+ ['abc', 'abc', 'abc', 'example 3'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ plan 2;
+ run_example('WeEeEkly', 'Wkly', 'Weekly', 'even repetitions');
+ run_example('WeEeEekly', 'Wekly', 'Weeekly', 'odd repetitions');
+ }) : pass 'skip tests';
+
+ exit;
+}