Tuesday, November 11, 2025

TWC347

Challenge Link

Task1

We format the date accordingly:
#!/usr/bin/env perl
use strict;
use warnings;

sub format_date{
  my @s = split ' ',$_[0];
  my $months = "JanFebMarAprMayJunJulAugSepOctNovDec";
  my $day = substr $s[0],0,length($s[0]) - 2;
  my $month = index($months,$s[1]) / 3 + 1;
  sprintf "%s-%02d-%02d",$s[2],$month,$day
}

printf "%s\n",format_date("1st Jan 2025");
printf "%s\n",format_date("22nd Feb 2025");
printf "%s\n",format_date("15th Apr 2025");
printf "%s\n",format_date("23rd Oct 2025");
printf "%s\n",format_date("31st Dec 2025");

Task2

We format the phone number accordingly:
#!/usr/bin/env perl
use strict;
use warnings;

sub format_phone_number{
  my ($phone) = @_;
  s/[- ]//g,
  s/(...)/$1-/g,
  s/-(.?)$/$1/,
  s/(^|-)(\d{2})(\d{2})$/$1$2-$3/ for $phone;
  $phone
}

printf "%s\n",format_phone_number('1-23-45-6');
printf "%s\n",format_phone_number('1234');
printf "%s\n",format_phone_number('12 345-6789');
printf "%s\n",format_phone_number('123 4567');
printf "%s\n",format_phone_number('123 456-78')

0 comments:

Post a Comment