From d8369f7f6a48b89f150482a90531edbb67d3d490 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 8 Nov 2021 21:54:58 +0100 Subject: Solution to task 1 --- challenge-138/jo-37/perl/ch-1.pl | 91 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 challenge-138/jo-37/perl/ch-1.pl diff --git a/challenge-138/jo-37/perl/ch-1.pl b/challenge-138/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..da396433b8 --- /dev/null +++ b/challenge-138/jo-37/perl/ch-1.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use DateTime; +use experimental 'signatures'; +# For testing only: +use DateTime::Duration; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <new(year => $ARGV[0]), + DateTime->new(year => $ARGV[0] + 1)); + + +### Implementation + +# Find the number of workdays in the interval from $start to (not +# including) $end. Expecting DateTime arguments. +sub workdays_int ($start, $end) { + return 0 if $start >= $end; + my $days = $end->delta_days($start)->in_units('days'); + # Sat is 5, Sun is 6. + my $dow = $start->day_of_week_0; + + # Every full week has 5 workdays. From the remaining days count + # working days only. + 5 * int($days / 7) + grep abs($dow + $_ - 5.5) > 1, 0 .. $days % 7 - 1; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is workdays_int(DateTime->new(year => 2021), + DateTime->new(year => 2022)), 261, 'example 1'; + is workdays_int(DateTime->new(year => 2020), + DateTime->new(year => 2021)), 262, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + my $m = DateTime->new( + year => 2021, + month => 11, + day => 8); # Challenge 138 published on Mon + my $d = DateTime::Duration->new(days => 1); + is workdays_int($m, $m), 0, 'same day'; + is workdays_int($m, $m + 4*$d), 4, 'Mon to Fri'; + is workdays_int($m, $m + 5*$d), 5, 'Mon to Sat'; + is workdays_int($m, $m + 6*$d), 5, 'Mon to Sun'; + is workdays_int($m + 3*$d, $m + 17*$d), 10, 'two full weeks'; + is workdays_int($m + 3*$d, $m + 5*$d), 2, 'Thu to Sat'; + is workdays_int($m + 3*$d, $m + 6*$d), 2, 'Thu to Sun'; + is workdays_int($m + 3*$d, $m + 7*$d), 2, 'Thu to Mon'; + is workdays_int($m + 4*$d, $m + 5*$d), 1, 'Fri to Sat'; + is workdays_int($m + 4*$d, $m + 6*$d), 1, 'Fri to Sun'; + is workdays_int($m + 4*$d, $m + 7*$d), 1, 'Fri to Mon'; + is workdays_int($m + 5*$d, $m + 7*$d), 0, 'Sat to Mon'; + is workdays_int($m + 6*$d, $m + 7*$d), 0, 'Sun to Mon'; + is workdays_int($m + 6*$d, $m + 12*$d), 5, 'Sun to Sat'; + is workdays_int($m + $d, $m), 0, 'end before start'; + } + + done_testing; + exit; +} -- cgit From 44f14b09f2e49649d8de2e213eaded0e6f4fc66b Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 9 Nov 2021 13:49:51 +0100 Subject: Solution to task 2 --- challenge-138/jo-37/perl/ch-2.pl | 87 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 challenge-138/jo-37/perl/ch-2.pl diff --git a/challenge-138/jo-37/perl/ch-2.pl b/challenge-138/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..bf1930ebec --- /dev/null +++ b/challenge-138/jo-37/perl/ch-2.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <