aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-11-14 17:12:56 +0000
committerGitHub <noreply@github.com>2025-11-14 17:12:56 +0000
commit0c3aba10381cb3ee83d9954bd073b9110a73bca4 (patch)
tree2ebb8e75cc1de2a078d9b3045281cae03c32621c
parente4f95af26374a568704a6256fd8a94343961f62e (diff)
parent029a680ebf817193dd5a619dca2f5dbcdebe6b0c (diff)
downloadperlweeklychallenge-club-0c3aba10381cb3ee83d9954bd073b9110a73bca4.tar.gz
perlweeklychallenge-club-0c3aba10381cb3ee83d9954bd073b9110a73bca4.tar.bz2
perlweeklychallenge-club-0c3aba10381cb3ee83d9954bd073b9110a73bca4.zip
Merge pull request #13027 from jo-37/contrib
Solutions to challenge 347
-rw-r--r--challenge-347/jo-37/blog.txt1
-rwxr-xr-xchallenge-347/jo-37/perl/ch-1.pl92
-rwxr-xr-xchallenge-347/jo-37/perl/ch-2.pl98
3 files changed, 191 insertions, 0 deletions
diff --git a/challenge-347/jo-37/blog.txt b/challenge-347/jo-37/blog.txt
new file mode 100644
index 0000000000..cb2433118b
--- /dev/null
+++ b/challenge-347/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2025/11/14/ch-347.html
diff --git a/challenge-347/jo-37/perl/ch-1.pl b/challenge-347/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..d1e46a64b6
--- /dev/null
+++ b/challenge-347/jo-37/perl/ch-1.pl
@@ -0,0 +1,92 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 -no_srand;
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+use DateTime::Format::Strptime;
+use DateTime::Format::DateParse;
+
+### 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 - format date
+
+ usage: $0 [-examples] [-tests] [DATE]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ DATE
+ a date parseable by Date::Parse
+
+ EOS
+}
+
+
+### Input and Output
+
+say format_date("@ARGV");
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/11/14/ch-347.html#task-1
+
+sub format_date {
+ state $fmt = DateTime::Format::Strptime->new(pattern => '%F');
+ DateTime::Format::DateParse->parse_datetime(shift)->set_formatter($fmt);
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name, $reason=undef) {
+ my $todo = $reason ? todo $reason : undef;
+ my $result = format_date(@$args);
+ is $result, $expected,
+ "$name: (@$args) -> " . $expected->name;
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [["1st Jan 2025"], string("2025-01-01"), 'example 1'],
+ [["22nd Feb 2025"], string("2025-02-22"), 'example 2'],
+ [["15th Apr 2025"], string("2025-04-15"), 'example 3'],
+ [["23rd Oct 2025"], string("2025-10-23"), 'example 4'],
+ [["31st Dec 2025"], string("2025-12-31"), 'example 5'],
+ );
+ plan scalar @examples;
+ run_example @$_ for @examples;
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ plan 1;
+ like dies {format_date("29th Feb 2025")}, qr/invalid/i, 'invalid date';
+ }) : pass 'skip tests';
+
+ exit;
+}
diff --git a/challenge-347/jo-37/perl/ch-2.pl b/challenge-347/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..22d93a1db8
--- /dev/null
+++ b/challenge-347/jo-37/perl/ch-2.pl
@@ -0,0 +1,98 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 -no_srand;
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use warnings FATAL => 'all';
+use Data::Dump qw(dd pp);
+use experimental 'signatures';
+
+use List::Gather;
+
+### 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 - format phone number
+
+ usage: $0 [-examples] [-tests] [PHONE]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ PHONE
+ a string containing a single phone number
+
+ EOS
+}
+
+
+### Input and Output
+
+say format_phone_number("@ARGV");
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/11/14/ch-347.html#task-2
+
+sub format_phone_number {
+ join '-', gather {
+ shift =~ tr/0-9//cdr =~
+ /(?:(...)(?!.$)(?{take $+}))*(?:(..)(?{take $+}))*/;
+ }
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name, $reason=undef) {
+ my $todo = $reason ? todo $reason : undef;
+ my $result = format_phone_number(@$args);
+ is $result, $expected,
+ "$name: (@$args) -> $expected";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [["1-23-45-6"], "123-456", 'example 1'],
+ [["1234"], "12-34", 'example 2'],
+ [["12 345-6789"], "123-456-789", 'example 3'],
+ [["123 4567"], "123-45-67", 'example 4'],
+ [["123 456-78"], "123-456-78", 'example 5'],
+ );
+ plan scalar @examples;
+ run_example @$_ for @examples;
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my @tests = (
+ [["1234567890"], "123-456-78-90", 'multiple matches'],
+ );
+ plan scalar @tests;
+ run_example @$_ for @tests;
+ }) : pass 'skip tests';
+
+ exit;
+}