diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-26 18:46:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-26 18:46:34 +0100 |
| commit | c281ebf5f94df784f0b1328acffc244e3baae4a7 (patch) | |
| tree | 2d529af6e288045c313585f184f9555b07a2a75d | |
| parent | ca37435975b6b72bbeaa6f8fa7bb9d315d557658 (diff) | |
| parent | 760579fdf00242a99734641b13028fdd194fd040 (diff) | |
| download | perlweeklychallenge-club-c281ebf5f94df784f0b1328acffc244e3baae4a7.tar.gz perlweeklychallenge-club-c281ebf5f94df784f0b1328acffc244e3baae4a7.tar.bz2 perlweeklychallenge-club-c281ebf5f94df784f0b1328acffc244e3baae4a7.zip | |
Merge pull request #8448 from oWnOIzRi/week227
add solution week 227 task 1 in perl
| -rw-r--r-- | challenge-227/steven-wilson/perl/ch-01.pl | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-227/steven-wilson/perl/ch-01.pl b/challenge-227/steven-wilson/perl/ch-01.pl new file mode 100644 index 0000000000..dd74320843 --- /dev/null +++ b/challenge-227/steven-wilson/perl/ch-01.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use 5.12.0; +use DateTime; +use Test::More; +use Test::Exception; + +# Key is the 1st January day of week name for given year. +# Value index 0 for normal year and index 1 for leap year. +my %friday_13ths = ( + "Sunday" => [ 2, 2 ], + "Monday" => [ 2, 2 ], + "Tuesday" => [ 2, 1 ], + "Wednesday" => [ 1, 2 ], + "Thursday" => [ 3, 2 ], + "Friday" => [ 1, 1 ], + "Saturday" => [ 1, 1 ], +); + +cmp_ok( unlucky_days(1753), "==", 2, "Test 1753 is 2" ); +cmp_ok( unlucky_days(1970), "==", 3, "Test 1970 is 3" ); +cmp_ok( unlucky_days(2023), "==", 2, "Test 2023 is 2" ); +cmp_ok( unlucky_days(9999), "==", 1, "Test 9999 is 1" ); +dies_ok { unlucky_days(1752) } "Test 1752 dies_ok"; +done_testing(); + +sub unlucky_days { + my $year = shift; + if ( $year < 1753 || $year > 9999 ) { + die "Year not in range: $year"; + } + my $dt = DateTime->new( + year => $year, + month => 1, + day => 1, + ); + my $first_day_of_year = $dt->day_name(); + my $is_leap_year = $dt->is_leap_year(); + return $friday_13ths{$first_day_of_year}[$is_leap_year]; +} |
