aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-11-11 13:53:50 +0000
committerGitHub <noreply@github.com>2025-11-11 13:53:50 +0000
commit2cb0be255278306d7da1abff79a9fb82ff37e7f6 (patch)
tree4aafc370ab72d05209393ed2f38f8b1b76af6f0c
parent47a660a0dddab8b591f6c69ddef3596c23be5f13 (diff)
parent269b3279c8976f286ad8eb137699827947210428 (diff)
downloadperlweeklychallenge-club-2cb0be255278306d7da1abff79a9fb82ff37e7f6.tar.gz
perlweeklychallenge-club-2cb0be255278306d7da1abff79a9fb82ff37e7f6.tar.bz2
perlweeklychallenge-club-2cb0be255278306d7da1abff79a9fb82ff37e7f6.zip
Merge pull request #13018 from jeanluc2020/jeanluc2020-347
Add solution 347.
-rw-r--r--challenge-347/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-347/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-347/jeanluc2020/perl/ch-1.pl102
-rwxr-xr-xchallenge-347/jeanluc2020/perl/ch-2.pl84
4 files changed, 188 insertions, 0 deletions
diff --git a/challenge-347/jeanluc2020/blog-1.txt b/challenge-347/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..afb84a983f
--- /dev/null
+++ b/challenge-347/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-347-1.html
diff --git a/challenge-347/jeanluc2020/blog-2.txt b/challenge-347/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..1ab7770ba9
--- /dev/null
+++ b/challenge-347/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-347-2.html
diff --git a/challenge-347/jeanluc2020/perl/ch-1.pl b/challenge-347/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..a79cc7afa1
--- /dev/null
+++ b/challenge-347/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,102 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/#TASK1
+#
+# Task 1: Format Date
+# ===================
+#
+# 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"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Since we can pick the year as-is, we just need to translate
+# the day and month. So we get the index of the saved strings
+# for both in an array, add one to get the day/month as a number,
+# and add a leading 0 if necessary.
+
+use v5.36;
+
+my @DAYS = get_days();
+my @MONTHS = get_months();
+my @YEARS = (1900..2100);
+
+format_date("1st Jan 2025");
+format_date("22nd Feb 2025");
+format_date("15th Apr 2025");
+format_date("23rd Oct 2025");
+format_date("31st Dec 2025");
+
+sub format_date($str) {
+ say "Input: '$str'";
+ my ($d, $m, $y) = split /\s+/, $str;
+ my $output = "${y}-" . indexed($m, @MONTHS) . "-" . indexed($d, @DAYS);
+ say "Output: '$output'";
+}
+
+sub indexed($str, @array) {
+ foreach my $i (0..$#array) {
+ if($str eq $array[$i]) {
+ return sprintf("%02d", $i+1);
+ }
+ }
+ return sprintf("%02d", 0); # fallback
+}
+
+sub get_days() {
+ my @DAYS = ("1st", "2nd", "3rd");
+ foreach(4..20) {
+ push @DAYS, "${_}th";
+ }
+ push @DAYS, "21st";
+ push @DAYS, "22nd";
+ push @DAYS, "23rd";
+ foreach(24..30) {
+ push @DAYS, "${_}th";
+ }
+ push @DAYS, "31st";
+ return @DAYS;
+}
+
+sub get_months() {
+ return ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
+}
+
diff --git a/challenge-347/jeanluc2020/perl/ch-2.pl b/challenge-347/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..8196ad1b23
--- /dev/null
+++ b/challenge-347/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,84 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/#TASK2
+#
+# Task 2: Format Phone Number
+# ===========================
+#
+# 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"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First, we remove all whitespace and dashes. Then, we fill an
+# array with the first three digits, keeping the remainder, unless
+# there are 4 or less digits left, in which case we pick either two
+# times two, or the remaining 3 or 2 digits.
+
+use v5.36;
+
+format_phone_numbers("1-23-45-6");
+format_phone_numbers("1234");
+format_phone_numbers("12 345-6789");
+format_phone_numbers("123 4567");
+format_phone_numbers("123 456-78");
+
+sub format_phone_numbers($phone) {
+ say "Input: '$phone'";
+ $phone =~ s/[\s-]*//g;
+ my @parts = ();
+ while(length($phone)) {
+ if(length($phone) > 4) {
+ push @parts, substr($phone, 0, 3, "");
+ } elsif (length($phone) == 4) {
+ push @parts, substr($phone, 0, 2, "");
+ push @parts, $phone;
+ $phone = "";
+ } else {
+ push @parts, $phone;
+ $phone = "";
+ }
+ }
+ say "Output: '" . join("-", @parts) . "'";
+}