From 4e461074a87de06f8b4a0ee6920716302c2a3505 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Wed, 19 Jun 2019 15:06:56 -0400 Subject: Challenge 13 by Jaldhar H. Vyas --- challenge-013/jaldhar-h-vyas/perl5/ch-1.pl | 59 ++++++++++++++++++++++++++++++ challenge-013/jaldhar-h-vyas/perl5/ch-2.pl | 26 +++++++++++++ challenge-013/jaldhar-h-vyas/perl6/ch-1.p6 | 41 +++++++++++++++++++++ challenge-013/jaldhar-h-vyas/perl6/ch-2.p6 | 22 +++++++++++ 4 files changed, 148 insertions(+) create mode 100755 challenge-013/jaldhar-h-vyas/perl5/ch-1.pl create mode 100755 challenge-013/jaldhar-h-vyas/perl5/ch-2.pl create mode 100755 challenge-013/jaldhar-h-vyas/perl6/ch-1.p6 create mode 100755 challenge-013/jaldhar-h-vyas/perl6/ch-2.p6 diff --git a/challenge-013/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-013/jaldhar-h-vyas/perl5/ch-1.pl new file mode 100755 index 0000000000..e0555ede34 --- /dev/null +++ b/challenge-013/jaldhar-h-vyas/perl5/ch-1.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; + +sub usage() { +print<<"-USAGE-"; +Usage: + $0 + + A four digit year greater than 1752 +-USAGE- + exit(0); +} + +sub elapsedLeapDays { + my ($year) = @_; + + $year -= 1752; # We want 1-based years here. + return int($year / 4) - int($year / 100) + int($year / 400); +} + +sub isLeap { + my ($year) = @_; + + # years divisible by 100 are not leap years unless they are divisble by 400. + return ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)); +} + +my $year = shift // usage(); + +# BUG: Is only accurate for years per the Gregorian calendar. Great Britain +# and her colonies adopted the Gregorian calendar in September 1752. Other +# locales did so at different dates but this script does not take any of +# that into account and just restricts input to 1753 onwards. +# +if ($year < 1753) { + usage; +} + +my $elapsedDays = ($year - 1753) * 365 + elapsedLeapDays($year); +my $newYearDay = ($elapsedDays + 1) % 7; # +1 because 1753/1/1 was a Monday. + +my @lastDays = (30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364); +# account for Feb 29 in leap years. +if (isLeap($year)) { + for my $month (1 .. 11) { + $lastDays[$month]++; + } +} +my @offset = (2, 3, 4, 5, 6, 0, 1); + + for my $month (0 .. 11) { + my $lastDay = $lastDays[$month]; + my $lastFriday = $lastDay - $offset[($newYearDay + $lastDay) % 7]; + + my $day = $lastFriday - (($month > 0) ? $lastDays[$month - 1] : -1); + say join q{/}, ($year, $month + 1, $day); +} diff --git a/challenge-013/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-013/jaldhar-h-vyas/perl5/ch-2.pl new file mode 100755 index 0000000000..9393417681 --- /dev/null +++ b/challenge-013/jaldhar-h-vyas/perl5/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; + +sub female { + my ($n) = @_; + + return ($n == 0) ? 1 : $n - male(female($n - 1)); +} + +sub male { + my ($n) = @_; + + return ($n == 0) ? 0 : $n - female(male($n - 1)); +} + +my (@f, @m); + +for my $n (0 .. 20) { + push @f, female($n); + push @m, male($n); +} + +say 'Female: ', join ', ', @f; +say 'Male: ', join ', ', @m; diff --git a/challenge-013/jaldhar-h-vyas/perl6/ch-1.p6 b/challenge-013/jaldhar-h-vyas/perl6/ch-1.p6 new file mode 100755 index 0000000000..fe00df931d --- /dev/null +++ b/challenge-013/jaldhar-h-vyas/perl6/ch-1.p6 @@ -0,0 +1,41 @@ +#!/usr/bin/perl6 + +sub elapsedLeapDays(Int $year) { + my $yr = $year - 1752; # We want 1-based years here. + return $yr div 4 - $yr div 100 + $yr div 400; +} + +sub isLeap(Int $year) { + # years divisible by 100 are not leap years unless they are divisble by 400. + return ($year %% 4 && (!$year %% 100 || $year %% 400)); +} + +# BUG: Is only accurate for years per the Gregorian calendar. Great Britain +# and her colonies adopted the Gregorian calendar in September 1752. Other +# locales did so at different dates but this script does not take any of +# that into account and just restricts input to 1753 onwards. +# +multi sub MAIN( + Int $year where { $_ > 1752 } #= A four digit year greater than 1752 +) { + + my $elapsedDays = ($year - 1753) * 365 + elapsedLeapDays($year); + my $newYearDay = ($elapsedDays + 1) % 7; # +1 because 1753/1/1 was a Monday. + + my @lastDays = (30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364); + # account for Feb 29 in leap years. + if isLeap($year) { + for 1 .. 11 -> $month { + @lastDays[$month]++; + } + } + my @offset = (2, 3, 4, 5, 6, 0, 1); + + for 0 .. 11 -> $month { + my $lastDay = @lastDays[$month]; + my $lastFriday = $lastDay - @offset[($newYearDay + $lastDay) % 7]; + + my $day = $lastFriday - (($month > 0) ?? @lastDays[$month - 1] !! -1); + ($year, $month + 1, $day).join('/').say; + } +} diff --git a/challenge-013/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-013/jaldhar-h-vyas/perl6/ch-2.p6 new file mode 100755 index 0000000000..6c9e478a60 --- /dev/null +++ b/challenge-013/jaldhar-h-vyas/perl6/ch-2.p6 @@ -0,0 +1,22 @@ +#!/usr/bin/perl6 + +multi sub female(Int $n where { $_ == 0 }) { + return 1; +} + +multi sub female(Int $n where { $_ > 0 }) { + return $n - male(female($n - 1)); +} + +multi sub male(Int $n where { $_ == 0 }) { + return 0; +} + +multi sub male(Int $n where { $_ > 0 }) { + return $n - female(male($n - 1)); +} + +multi sub MAIN() { + say 'Female: ', ({ female((state $n = 0)++) } ... *)[0 .. 20].join(', '); + say 'Male: ', ({ male((state $n = 0)++) } ... *)[0 .. 20].join(', '); +} -- cgit