aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-07 22:43:57 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-11 22:58:11 +0200
commit334123cb99d7eea1e599acb3c5b90bfd59531660 (patch)
tree089dca8f91815127cf6244be7ca4ae5ea322e9e3
parent063a0771670593aa3c1305704d02145277384878 (diff)
downloadperlweeklychallenge-club-334123cb99d7eea1e599acb3c5b90bfd59531660.tar.gz
perlweeklychallenge-club-334123cb99d7eea1e599acb3c5b90bfd59531660.tar.bz2
perlweeklychallenge-club-334123cb99d7eea1e599acb3c5b90bfd59531660.zip
Solution to task 2
-rwxr-xr-xchallenge-329/jo-37/perl/ch-2.pl111
1 files changed, 111 insertions, 0 deletions
diff --git a/challenge-329/jo-37/perl/ch-2.pl b/challenge-329/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..d4e6c38760
--- /dev/null
+++ b/challenge-329/jo-37/perl/ch-2.pl
@@ -0,0 +1,111 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 -no_srand;
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+use Set::Scalar;
+use List::UtilsBy 'max_by';
+
+### 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 - nice 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 nice_string(shift);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/07/11/ch-329.html#task-2
+
+sub split_nice ($str) {
+ my $nice = Set::Scalar->new($str =~ /\p{Ll}/g) *
+ Set::Scalar->new(map lc, $str =~ /\p{Lu}/g);
+ return () if $nice->is_null;
+ my @parts = grep $_, split qr{[^ @$nice]+}ixx, $str;
+ @parts == 1 ? @parts : map split_nice($_), @parts;
+}
+
+sub nice_string ($str) {
+ (max_by {length} split_nice($str)) // '';
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = nice_string(@$args);
+ is $result, $expected,
+ qq{$name: "@$args" -> "$expected"};
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [["YaaAho"], "aaA", 'example 1'],
+ [["cC"], "cC", 'example 2'],
+ [["A"], "", 'example 3'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my @tests = (
+ [["abdAcAaAcBbdcabD"], "AaA", 'recurse substrings'],
+ [["aA1BBb"], "BBb", 'non-letter'],
+ [["abcDd"], "Dd", 'non-nice head'],
+ [["Ddefg"], "Dd", 'non-nice tail'],
+ [["AacbB"], "Aa", 'non-unique substring'],
+ [["abAcBab"], "", 'segmented'],
+ [["aA Bb"], "aA", 'blank'],
+ [["äÄÖöüÜßSs"], "äÄÖöüÜ", 'umlauts'],
+ [["aAxaAbBxaAbBcCxaAbBcCdD"], "aAbBcCdD", 'multiple substrings'],
+ );
+ plan scalar @tests;
+ for (@tests) {
+ run_example @$_;
+ }
+ }) : pass 'skip tests';
+
+ exit;
+}