aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-11-10 19:06:50 +0000
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-11-10 19:06:50 +0000
commita5e4e82d7ff72e451ba6333f83e8222c96ec1d46 (patch)
tree1318f2f158edf627eeed0e2df68f8c4b114edb07
parent2cb74a7aa10c72b7862513e5e320bc29f1d5f55f (diff)
parent7e06f06aec7f78c813797376a7d93d6a2369bb3b (diff)
downloadperlweeklychallenge-club-a5e4e82d7ff72e451ba6333f83e8222c96ec1d46.tar.gz
perlweeklychallenge-club-a5e4e82d7ff72e451ba6333f83e8222c96ec1d46.tar.bz2
perlweeklychallenge-club-a5e4e82d7ff72e451ba6333f83e8222c96ec1d46.zip
Merge remote-tracking branch 'refs/remotes/origin/master'
-rw-r--r--challenge-347/wanderdoc/perl/ch-1.pl72
-rw-r--r--challenge-347/wanderdoc/perl/ch-2.pl72
2 files changed, 144 insertions, 0 deletions
diff --git a/challenge-347/wanderdoc/perl/ch-1.pl b/challenge-347/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..69396873d5
--- /dev/null
+++ b/challenge-347/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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)
+
+
+Example 1
+
+Input: $str = "1st Jan 2025"
+Output: "2025-01-01"
+
+
+Example 2
+
+Input: $str = "22nd Feb 2025"
+Output: "2025-02-22"
+
+
+Example 3
+
+Input: $str = "15th Apr 2025"
+Output: "2025-04-15"
+
+
+Example 4
+
+Input: $str = "23rd Oct 2025"
+Output: "2025-10-23"
+
+
+Example 5
+
+Input: $str = "31st Dec 2025"
+Output: "2025-12-31"
+=cut
+
+
+
+use Test2::V0 -no_srand => 1;
+
+
+is(format_date('1st Jan 2025'), '2025-01-01', 'Example 1');
+is(format_date('22nd Feb 2025'), '2025-02-22', 'Example 2');
+is(format_date('15th Apr 2025'), '2025-04-15', 'Example 3');
+is(format_date('23rd Oct 2025'), '2025-10-23', 'Example 4');
+is(format_date('31st Dec 2025'), '2025-12-31', 'Example 5');
+done_testing();
+
+sub format_date
+{
+ my $str = $_[0];
+ my %days = map { /1$/ ? $_ . 'st' :
+ /2$/ ? $_ . 'nd' :
+ /3$/ ? $_ . 'rd' :
+ $_ . 'th', $_ } 1 .. 31;
+ my @months = qw(dummy Jan Feb Mar Apr Mai Jun Jul Auf Sep Oct Nov Dec);
+ my %months = map { $months[$_], $_ } 1 .. $#months;
+ my ($this_day, $this_month, $this_year) = split(/ /, $str);
+
+ return
+ join('-',
+ $this_year, sprintf("%02d", $months{$this_month}),
+ sprintf("%02d",$days{$this_day}));
+}
diff --git a/challenge-347/wanderdoc/perl/ch-2.pl b/challenge-347/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..5791183c6e
--- /dev/null
+++ b/challenge-347/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,72 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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
+
+
+Example 1
+
+Input: $phone = "1-23-45-6"
+Output: "123-456"
+
+
+Example 2
+
+Input: $phone = "1234"
+Output: "12-34"
+
+
+Example 3
+
+Input: $phone = "12 345-6789"
+Output: "123-456-789"
+
+
+Example 4
+
+Input: $phone = "123 4567"
+Output: "123-45-67"
+
+
+Example 5
+
+Input: $phone = "123 456-78"
+Output: "123-456-78"
+=cut
+
+
+use Test2::V0 -no_srand => 1;
+is(format_phone_number("1-23-45-6"), '123-456', 'Example 1');
+is(format_phone_number("1234"), '12-34', 'Example 2');
+is(format_phone_number("12 345-6789"), '123-456-789', 'Example 3');
+is(format_phone_number("123 4567"), '123-45-67', 'Example 4');
+is(format_phone_number("123 456-78"), '123-456-78', 'Example 5');
+done_testing();
+
+
+sub format_phone_number
+{
+ my $phone = $_[0];
+ $phone =~ tr/- //ds;
+
+ $phone =~ s/(\d{3})(?=\d{2,4})/$1-/g;
+ my @arr = split(/\-/, $phone);
+ my $tail = pop @arr;
+
+ $tail = length($tail) < 4 ? $tail :
+ join('-', substr($tail, 0, 2), substr($tail, 2));
+ push @arr, $tail;
+ return join('-', @arr);
+}