Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions challenge-347/e-choroba/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/perl
use warnings;
use strict;
use experimental qw( signatures );

{
my @DAYS = map +(s/(?<!1)1$/1st/r
=~ s/(?<!1)2$/2nd/r
=~ s/(?<!1)3$/3rd/r
=~ s/(?<=\d)$/th/r),
1 .. 31;
my @MONTHS = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my @YEARS = 1900 .. 2100;

my %DAYS;
@DAYS{@DAYS} = ();
my $m = 1;
my %MONTHS = map +($_ => $m++), @MONTHS;
my %YEARS;
@YEARS{@YEARS} = ();

sub format_date($str) {
my ($day, $month, $year) = split / /, $str;
if ( exists $DAYS{$day}
&& exists $MONTHS{$month}
&& exists $YEARS{$year}
) {
return sprintf '%s-%02d-%02d', $year,
$MONTHS{$month},
$day =~ /([0-9]+)/
}
die "Date invalid or out of range: $str.\n"
}
}

use Test2::V0;
plan(5 + 1 + 5);

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';

is format_date('12th Dec 1900'), '1900-12-12', 'No 12nd';

for my $invalid ('11st Apr 2020', '12nd Apr 2020', '0th Apr 2000',
'1st Jan 2101', '31st Dec 1899'
) {
is dies { format_date($invalid) },
"Date invalid or out of range: $invalid.\n",
"Invalid $invalid";
}
22 changes: 22 additions & 0 deletions challenge-347/e-choroba/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/perl
use warnings;
use strict;
use experimental qw( signatures );

sub format_phone_number($phone) {
s/[- ]//g, # Remove spaces and dashes.
s/(...)/$1-/g, # Groups of 3.
s/-(.?)$/$1/, # - or -1 at the end.
s/(^|-)([0-9]{2})([0-9]{2})$/$1$2-$3/ # Split groups of 4 at the end.
for $phone;

return $phone
}

use Test::More tests => 5;

is format_phone_number('1-23-45-6'), '123-456', 'Example 1';
is format_phone_number('1234'), '12-34', 'Example 2';
is format_phone_number('12 345-6789'), '123-456-789', 'Example 3';
is format_phone_number('123 4567'), '123-45-67', 'Example 4';
is format_phone_number('123 456-78'), '123-456-78', 'Example 5';