diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-25 18:09:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-25 18:09:22 +0100 |
| commit | 3f8923e0abe92434145fe6bd35e77bf253a3bf7f (patch) | |
| tree | c351172cab624305562d071933e6182a7010ec00 | |
| parent | 410695fa864dea7e420f68cbfdcba4ede9fdd020 (diff) | |
| parent | 60a321de609bc5763d653250765f39398220eff1 (diff) | |
| download | perlweeklychallenge-club-3f8923e0abe92434145fe6bd35e77bf253a3bf7f.tar.gz perlweeklychallenge-club-3f8923e0abe92434145fe6bd35e77bf253a3bf7f.tar.bz2 perlweeklychallenge-club-3f8923e0abe92434145fe6bd35e77bf253a3bf7f.zip | |
Merge pull request #12408 from jo-37/contrib
Solutions to challenge 331
| -rw-r--r-- | challenge-331/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-331/jo-37/perl/ch-1.pl | 91 | ||||
| -rwxr-xr-x | challenge-331/jo-37/perl/ch-2.pl | 108 |
3 files changed, 200 insertions, 0 deletions
diff --git a/challenge-331/jo-37/blog.txt b/challenge-331/jo-37/blog.txt new file mode 100644 index 0000000000..345adbed36 --- /dev/null +++ b/challenge-331/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2025/07/25/ch-331.html diff --git a/challenge-331/jo-37/perl/ch-1.pl b/challenge-331/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..389838b6a0 --- /dev/null +++ b/challenge-331/jo-37/perl/ch-1.pl @@ -0,0 +1,91 @@ +#!/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); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV; + +sub usage { + die <<~EOS; + $0 - length of last word + + usage: $0 [-examples] [-tests] [STR...] + + -examples + run the examples from the challenge + + -tests + run some tests + + STR... + string or list of words + + EOS +} + + +### Input and Output + +say llw("@ARGV"); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/07/25/ch-331.html#task-1 + + +sub llw { + () = shift =~ /\w+/g or return 0; + $+[0] - $-[0]; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = llw(@$args); + is $result, $expected, + qq($name: "@$args" -> $expected); + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["The Weekly Challenge"], 9, 'example 1'], + [[" Hello World "], 5, 'example 2'], + [["Let's begin the fun"], 3, 'example 3'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 2; + is llw('... --- +++ /// '), 0, 'no word'; + is llw("it isn't"), 1, 'part only'; + }) : pass 'skip tests'; + + exit; +} diff --git a/challenge-331/jo-37/perl/ch-2.pl b/challenge-331/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..24f43eaf2c --- /dev/null +++ b/challenge-331/jo-37/perl/ch-2.pl @@ -0,0 +1,108 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 qw(!float -no_srand); +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use PDL v2.100; +use PDL::NiceSlice; + +### 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 == 2; + +sub usage { + die <<~EOS; + $0 - buddy strings + + usage: $0 [-examples] [-tests] [SOURCE TARGET] + + -examples + run the examples from the challenge + + -tests + run some tests + + SOURCE TARGET + two strings + + EOS +} + + +### Input and Output + +say +(qw(true false))[!buddy_strings(@ARGV)]; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/07/25/ch-331.html#task-2 + + +sub buddy_strings { + my $s = long map [map ord, split //], @_; + my $diff = which $s->xchg(0, 1)->bxorover; + + $diff->isempty && $s(,0)->uniq->dim(0) < $s->dim(0) || + $diff->nelem == 2 && all $s($diff,0) == $s($diff(-1:0),1); +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = buddy_strings(@$args); + is $result, $expected, + "$name: (@$args) -> " . $expected->name; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["fuck", "fcuk"], T(), 'example 1'], + [["love", "love"], F(), 'example 2'], + [["fodo", "food"], T(), 'example 3'], + [["feed", "feed"], T(), 'example 4'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + my @tests = ( + [["this", "thus"], F(), 'one different char'], + [["this", "then"], F(), 'two different chars'], + [["the", "thee"], F(), 'different lengths'], + [["seprű", "sűpre"], T(), 'beyond U+00FF'], + [["hour", "ouhr"], F(), 'rotated'], + [["hour", "ohru"], F(), 'two flips'], + [["aaa", "aaa"], T(), 'more non-unique chars'], + + ); + plan scalar @tests; + for (@tests) { + run_example @$_; + } + }) : pass 'skip tests'; + + exit; +} |
