Task 1: Format Date
"From Ordinal Text to ISO Date!"
The first task asks us to convert dates like 23rd Oct 2025 into ISO format 2025-10-23. The input contains day with ordinal suffix, a 3-letter month, and a 4-digit year.
The Strategy: Parse with regex into day/suffix/month/year parts, validate each field, confirm ordinal suffix correctness (including 11th/12th/13th exceptions), map month names to numbers, then format as YYYY-MM-DD.
Perl Implementation
sub format_date ($input) {
state $check = compile(Str);
($input) = $check->($input);
$input =~ s/^\s+|\s+$//g;
my ( $day, $suffix, $month_name, $year ) =
$input =~ /\A(\d{1,2})(st|nd|rd|th)\s+([A-Za-z]{3})\s+(\d{4})\z/
or die 'Invalid date format';
my $day_num = $day + 0;
die 'Day out of range' unless 1 <= $day_num && $day_num <= 31;
die 'Invalid ordinal suffix' unless _suffix_for($day_num) eq $suffix;
my $month_num = $MONTH_INDEX{$month_name} // die 'Unknown month abbreviation';
my $year_num = $year + 0;
die 'Year out of range' unless 1900 <= $year_num && $year_num <= 2100;
return sprintf '%04d-%02d-%02d', $year_num, $month_num, $day_num;
}
Python Implementation
def format_date(text: str) -> str:
"""Convert strings like '10th Nov 2025' to '2025-11-10'."""
match = DATE_PATTERN.match(text.strip())
if not match:
raise ValueError("Invalid date format")
day = int(match.group("day"))
suffix = match.group("suffix")
month_name = match.group("month")
year = int(match.group("year"))
if not 1 <= day <= 31:
raise ValueError("Day out of range")
if _suffix_for(day) != suffix:
raise ValueError("Invalid ordinal suffix")
month = MONTHS.get(month_name)
if month is None:
raise ValueError("Unknown month abbreviation")
if not 1900 <= year <= 2100:
raise ValueError("Year out of range")
return f"{year:04d}-{month:02d}-{day:02d}"