diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-04 10:55:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-04 10:55:28 +0100 |
| commit | 7e837894d71d696a07b9fdc7367c4c13c92da0ab (patch) | |
| tree | 5c09ecb56ffdc0aec5544a1ee6af3d3e034d73c4 | |
| parent | 2804e9bd3bb454781643a543de9b519c1c9d2c95 (diff) | |
| parent | 11dc96451373471d938393dc9dd4a48251b9059e (diff) | |
| download | perlweeklychallenge-club-7e837894d71d696a07b9fdc7367c4c13c92da0ab.tar.gz perlweeklychallenge-club-7e837894d71d696a07b9fdc7367c4c13c92da0ab.tar.bz2 perlweeklychallenge-club-7e837894d71d696a07b9fdc7367c4c13c92da0ab.zip | |
Merge pull request #12279 from jo-37/contrib
Solutions to challenge 328
| -rw-r--r-- | challenge-328/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-328/jo-37/perl/ch-1.pl | 101 | ||||
| -rwxr-xr-x | challenge-328/jo-37/perl/ch-2.pl | 110 |
3 files changed, 212 insertions, 0 deletions
diff --git a/challenge-328/jo-37/blog.txt b/challenge-328/jo-37/blog.txt new file mode 100644 index 0000000000..c052b935b4 --- /dev/null +++ b/challenge-328/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2025/07/04/ch-328.html diff --git a/challenge-328/jo-37/perl/ch-1.pl b/challenge-328/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..798787cf22 --- /dev/null +++ b/challenge-328/jo-37/perl/ch-1.pl @@ -0,0 +1,101 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 -no_srand; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental qw(signatures vlb); + + +### Options and Arguments + +my ($tests, $examples, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV == 1; + +sub usage { + die <<~EOS; + $0 - replace all ? + + usage: $0 [-examples] [-tests] [STR] + + -examples + run the examples from the challenge + + -tests + run some tests + + STR + a string + + EOS +} + + +### Input and Output + +say replace_all(shift); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/07/04/ch-328.html#task-1 + +sub rc { + (\my %c)->@{'a' .. 'z'} = (); + delete @c{@_}; + + scalar each %c; +} + +sub replace_all ($str) { + 1 while $str =~ s/(?<=([^?]?))\?(?=([^?]?))/rc($1, $2)/e; + + $str; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($str, $name) { + my $result = replace_all($str); + my $pattern = $str =~ s/\?/./gr; + subtest("$name: $str -> $result" => sub { + plan 2; + like $result, qr/$pattern/, 'match characters'; + unlike $result, qr/(.)\1/, 'no repeated characters'; + }); + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + ['a?z', 'example 1'], + ['pe?k', 'example 2'], + ['gra?te', 'example 3'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 1; + run_example('a???b', 'adjacent question marks'); + }) : pass 'skip tests'; + + exit; +} 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; +} |
