aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2025-11-10 14:41:00 +0000
committerNiels van Dijke <perlboy@cpan.org>2025-11-10 14:41:00 +0000
commit6b20125b08bc07bcd1b4913a4020791ae37cb83a (patch)
treed8efb1f039f053754939e1a57d6b505bca6bba1d
parent00f12b64ee72fe59f860dcfd0c471b2d915b50cd (diff)
downloadperlweeklychallenge-club-6b20125b08bc07bcd1b4913a4020791ae37cb83a.tar.gz
perlweeklychallenge-club-6b20125b08bc07bcd1b4913a4020791ae37cb83a.tar.bz2
perlweeklychallenge-club-6b20125b08bc07bcd1b4913a4020791ae37cb83a.zip
w347 - Task 1 & 2 (Perl)
-rwxr-xr-xchallenge-347/perlboy1967/perl/ch1.pl40
-rwxr-xr-xchallenge-347/perlboy1967/perl/ch2.pl51
2 files changed, 91 insertions, 0 deletions
diff --git a/challenge-347/perlboy1967/perl/ch1.pl b/challenge-347/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..88a583e9ec
--- /dev/null
+++ b/challenge-347/perlboy1967/perl/ch1.pl
@@ -0,0 +1,40 @@
+#!/bin/perl
+
+=pod
+
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-347#TASK1>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Format Date
+Submitted by: Mohammad Sajid Anwar
+
+You are given a date in the form: 10th Nov 2025.
+
+Write a script to format the given date in the form: 2025-11-10 using the set below.
+
+|| @DAYS = ("1st", "2nd", "3rd", ....., "30th", "31st")
+|| @MONTHS = ("Jan", "Feb", "Mar", ....., "Nov", "Dec")
+|| @YEARS = (1900..2100)
+
+=cut
+
+use Test2::V0 qw(-no_srand);
+use exact 'v5.32', -signatures;
+
+sub formatData ($str) {
+ state $M = { 'Jan' => 1, 'Feb' => 2, 'Mar' => 3,
+ 'Apr' => 4, 'May' => 5, 'Jun' => 6,
+ 'Jul' => 7, 'Aug' => 8, 'Sep' => 9,
+ 'Oct' =>10, 'Nov' =>11, 'Dec' =>12};
+ return sprintf('%d-%02d-%02d',$3,$M->{$2},$1)
+ if $str =~ m/(\d+)\S+\s+([A-Z][a-z][a-z])\s+(\d+)/;
+}
+
+is(formatData('1st Jan 2025'),'2025-01-01','Example 1');
+is(formatData('22nd Feb 2025'),'2025-02-22','Example 2');
+is(formatData('15th Apr 2025'),'2025-04-15','Example 3');
+is(formatData('23rd Oct 2025'),'2025-10-23','Example 4');
+is(formatData('31st Dec 2025'),'2025-12-31','Example 5');
+
+done_testing;
diff --git a/challenge-347/perlboy1967/perl/ch2.pl b/challenge-347/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..2a0d95021d
--- /dev/null
+++ b/challenge-347/perlboy1967/perl/ch2.pl
@@ -0,0 +1,51 @@
+#!/bin/perl
+
+=pod
+
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-347#TASK2>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Format Phone Number
+Submitted by: Mohammad Sajid Anwar
+
+You are given a phone number as a string containing digits, space and dash only.
+
+Write a script to format the given phone number using the below rules:
+
+1. Removing all spaces and dashes
+2. Grouping digits into blocks of length 3 from left to right
+3. Handling the final digits (4 or fewer) specially:
+- 2 digits: one block of length 2
+- 3 digits: one block of length 3
+- 4 digits: two blocks of length 2
+4. Joining all blocks with dashes
+
+=cut
+
+use Test2::V0 qw(-no_srand);
+use exact 'v5.32', -signatures;
+
+sub formatPhoneNumber ($str) {
+ $str =~ s/\D+//g;
+
+ # Split the string in three character substrings
+ my @p = unpack('(A3)*',$str);
+
+ # The remainder is in $p[-1]
+ # Length is either one or two.
+ # Two is okay, one needs 'borrowing at $p[-2]
+ if (length $p[-1] == 1) {
+ push(@p,unpack('(A2)*',join('',reverse(pop(@p),pop(@p))));
+ }
+
+ return join('-',@p);
+}
+
+is(formatPhoneNumber('1-23-45-6'),'123-456','Example 1');
+is(formatPhoneNumber('1234'),'12-34','Example 2');
+is(formatPhoneNumber('12 345-6789'),'123-456-789','Example 3');
+is(formatPhoneNumber('123 4567'),'123-45-67','Example 4');
+is(formatPhoneNumber('123 456-78'),'123-456-78','Example 5');
+
+done_testing;