diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-11 15:22:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-11 15:22:52 +0000 |
| commit | 4b9e920370d5b1441bb1ba9296c667b3a6d8088e (patch) | |
| tree | 610e99f547c30b1c4771a96a854d103516495872 | |
| parent | 76005cbd01437c33d14513ba62d3ef0f004ab74c (diff) | |
| parent | af944cebb4a1c004f450af4841f7dc7cf426f545 (diff) | |
| download | perlweeklychallenge-club-4b9e920370d5b1441bb1ba9296c667b3a6d8088e.tar.gz perlweeklychallenge-club-4b9e920370d5b1441bb1ba9296c667b3a6d8088e.tar.bz2 perlweeklychallenge-club-4b9e920370d5b1441bb1ba9296c667b3a6d8088e.zip | |
Merge pull request #13019 from mahnkong/challenge-347
Challenge 347
| -rw-r--r-- | challenge-347/mahnkong/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-347/mahnkong/perl/ch-2.pl | 22 |
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-347/mahnkong/perl/ch-1.pl b/challenge-347/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..8ae0f289af --- /dev/null +++ b/challenge-347/mahnkong/perl/ch-1.pl @@ -0,0 +1,41 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +my @DAYS = ( + "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", + "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", + "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", + "31st" +); + +my @MONTHS = ( + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" +); + +my @YEARS = (1900..2100); + +sub get_array_index_of_elem($value, @array) { + for (my $i = 0; $i < scalar(@array); $i++) { + return $i if ($array[$i] eq $value); + } + return undef; +} + +sub run($str) { + my ($day, $month, $year) = split / /, $str; + my $res_year = get_array_index_of_elem($year, @YEARS); + my $res_month = get_array_index_of_elem($month, @MONTHS); + my $res_day = get_array_index_of_elem($day, @DAYS); + + return undef if (! defined $res_year || ! defined $res_month || ! defined $res_day); + return sprintf("%s-%02d-%02d", $year, $res_month+1, $res_day+1); +} + +is(run("1st Jan 2025"), "2025-01-01", "Example 1"); +is(run("22nd Feb 2025"), "2025-02-22", "Example 2"); +is(run("15th Apr 2025"), "2025-04-15", "Example 3"); +is(run("23rd Oct 2025"), "2025-10-23", "Example 4"); +is(run("31st Dec 2025"), "2025-12-31", "Example 5"); diff --git a/challenge-347/mahnkong/perl/ch-2.pl b/challenge-347/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..f3dbeee3c0 --- /dev/null +++ b/challenge-347/mahnkong/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($phone) { + my @parts; + $phone =~ s/\D//g; + while (length($phone)) { + my $block_length = (length($phone) == 4 || length($phone) == 2) ? 2 : 3; + push @parts, substr($phone, 0, $block_length); + $phone = substr($phone, $block_length, length($phone)); + + } + return join("-", @parts); +} + +is(run("1-23-45-6"), "123-456", "Example 1"); +is(run("1234"), "12-34", "Example 2"); +is(run("12 345-6789"), "123-456-789", "Example 3"); +is(run("123 4567"), "123-45-67", "Example 4"); +is(run("123 456-78"), "123-456-78", "Example 5"); |
