aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-14 13:51:18 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-07-18 16:50:54 +0200
commit8593bc8aa1b11da04cca963c54e2430b8be41bff (patch)
tree6b0078db608813c2c30bac66305ccc7e54439fc7
parentc426fab2721595210e8e2ad3cef39a501b938e30 (diff)
downloadperlweeklychallenge-club-8593bc8aa1b11da04cca963c54e2430b8be41bff.tar.gz
perlweeklychallenge-club-8593bc8aa1b11da04cca963c54e2430b8be41bff.tar.bz2
perlweeklychallenge-club-8593bc8aa1b11da04cca963c54e2430b8be41bff.zip
Solution to task 1
-rwxr-xr-xchallenge-330/jo-37/perl/ch-1.pl95
1 files changed, 95 insertions, 0 deletions
diff --git a/challenge-330/jo-37/perl/ch-1.pl b/challenge-330/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..87cb0a3d16
--- /dev/null
+++ b/challenge-330/jo-37/perl/ch-1.pl
@@ -0,0 +1,95 @@
+#!/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 - clear digits
+
+ usage: $0 [-examples] [-tests] [STR]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ STR
+ a string
+
+ EOS
+}
+
+
+### Input and Output
+
+say clear_digits(shift);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/07/18/ch-330.html#task-1
+
+sub clear_digits ($str) {
+ 1 while $str =~ s/\P{N}?\p{N}//;
+ $str;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = clear_digits(@$args);
+ is $result, $expected,
+ "$name: '@$args' -> '$expected'";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [["cab12"], "c", 'example 1'],
+ [["xy99"], "", 'example 2'],
+ [["pa1erl"], "perl", 'example 3'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my @tests = (
+ [["12ab3"], "a", 'leading digits'],
+ [["ab¹c²d"], "ad", 'superscripts'],
+ );
+ plan scalar @tests;
+ for (@tests) {
+ run_example @$_;
+ }
+ }) : pass 'skip tests';
+
+ exit;
+}