aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@users.noreply.github.com>2025-11-10 19:19:14 +0100
committerGitHub <noreply@github.com>2025-11-10 19:19:14 +0100
commit64426eb24d1cc6a4d2c25ab87e80ab5b275c24a5 (patch)
tree50dca2c183e26cdacee52609ea61f79b17134c8b
parent83c02309afd45cdbfce02a54ee39361fce28c592 (diff)
downloadperlweeklychallenge-club-64426eb24d1cc6a4d2c25ab87e80ab5b275c24a5.tar.gz
perlweeklychallenge-club-64426eb24d1cc6a4d2c25ab87e80ab5b275c24a5.tar.bz2
perlweeklychallenge-club-64426eb24d1cc6a4d2c25ab87e80ab5b275c24a5.zip
Create ch-1.pl
-rw-r--r--challenge-347/wanderdoc/perl/ch-1.pl72
1 files changed, 72 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}));
+}