aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-29 14:14:50 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-08-01 08:57:57 +0200
commit99fcc63ac4fb8c30d03e4ce42bcd357504cd102b (patch)
treeee8303b2e5f5944c040055870e77baf44907f28b
parentf506c15b51deb761384d33fce37dc14fc715df53 (diff)
downloadperlweeklychallenge-club-99fcc63ac4fb8c30d03e4ce42bcd357504cd102b.tar.gz
perlweeklychallenge-club-99fcc63ac4fb8c30d03e4ce42bcd357504cd102b.tar.bz2
perlweeklychallenge-club-99fcc63ac4fb8c30d03e4ce42bcd357504cd102b.zip
Solution to task 2
-rwxr-xr-xchallenge-332/jo-37/perl/ch-2.pl100
1 files changed, 100 insertions, 0 deletions
diff --git a/challenge-332/jo-37/perl/ch-2.pl b/challenge-332/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..b3f0c6b957
--- /dev/null
+++ b/challenge-332/jo-37/perl/ch-2.pl
@@ -0,0 +1,100 @@
+#!/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 == 1;
+
+sub usage {
+ die <<~EOS;
+ $0 - odd letters
+
+ usage: $0 [-examples] [-tests] [STR]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ STR
+ a string
+
+ EOS
+}
+
+
+### Input and Output
+
+say +(qw(true false))[!odd_letters(shift)];
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/08/01/ch-332.html#task-2
+
+sub odd_letters {
+ shift !~ /
+ (.)
+ (?!(?=(.*))(\1.+(?=\g{-2}$)|(?<=(?=x^|(?-1)).)))
+ ((??{qr([^$1]*)})\1)
+ (?:(?-1){2})*
+ (?!.*\1)
+ /x;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = odd_letters(@$args);
+ is $result, $expected,
+ "$name: (@$args) -> " . $expected->name;
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [["weekly"], F(), 'example 1'],
+ [["perl"], T(), 'example 2'],
+ [["challenge"], F(), 'example 3'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my @tests = (
+ [["abccbabac"], T(), 'all multiple'],
+ [["abccbababc"], F(), 'all multiple, single even'],
+ );
+ plan scalar @tests;
+ for (@tests) {
+ run_example @$_;
+ }
+ }) : pass 'skip tests';
+
+ exit;
+}