diff options
| author | Adam Russell <ac.russell@live.com> | 2019-06-23 01:40:29 -0400 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2019-06-23 01:40:29 -0400 |
| commit | 8ebf9b8b1571591bf9e44ecc1ed7389234790c3d (patch) | |
| tree | 0739b18dd005d953f64e13c48b7c9daa57d98d64 /challenge-013 | |
| parent | 0129b707f95082868e7e0d8d722d16a15d3cb28f (diff) | |
| download | perlweeklychallenge-club-8ebf9b8b1571591bf9e44ecc1ed7389234790c3d.tar.gz perlweeklychallenge-club-8ebf9b8b1571591bf9e44ecc1ed7389234790c3d.tar.bz2 perlweeklychallenge-club-8ebf9b8b1571591bf9e44ecc1ed7389234790c3d.zip | |
solutions for challenge 013
Diffstat (limited to 'challenge-013')
| -rw-r--r-- | challenge-013/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-013/adam-russell/perl5/ch-1.pl | 44 | ||||
| -rw-r--r-- | challenge-013/adam-russell/perl5/ch-2.pl | 28 |
3 files changed, 73 insertions, 0 deletions
diff --git a/challenge-013/adam-russell/blog.txt b/challenge-013/adam-russell/blog.txt new file mode 100644 index 0000000000..41161087e0 --- /dev/null +++ b/challenge-013/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/4730.html diff --git a/challenge-013/adam-russell/perl5/ch-1.pl b/challenge-013/adam-russell/perl5/ch-1.pl new file mode 100644 index 0000000000..9d26f4f382 --- /dev/null +++ b/challenge-013/adam-russell/perl5/ch-1.pl @@ -0,0 +1,44 @@ +use strict; +use warnings; +## +# Write a script to print the date of last Friday of every month of a given year. +## +use constant YEAR => 2019; +use constant FRIDAY => 5; +use constant JANUARY => 1; +use constant FEBRUARY => 2; +use constant MARCH => 3; +use constant APRIL => 4; +use constant MAY => 5; +use constant JUNE => 6; +use constant JULY => 7; +use constant AUGUST => 8; +use constant SEPTEMBER => 9; +use constant OCTOBER => 10; +use constant NOVEMBER => 11; +use constant DECEMBER => 12; + +sub day_of_week{ + my($year, $month, $day) = @_; + my @month_value = (0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4); + $year-- if($month == JANUARY || $month == FEBRUARY); + return ($year + int($year/4) - int($year/100) + int($year/400) + $month_value[$month-1] + $day) % 7 +} + +## +# Main +## +my $is_leap_year = (((YEAR % 4) == 0) && ((YEAR % 100) != 0)) || ((YEAR % 4) == 0 && (YEAR % 400) == 0); +for my $m (JANUARY..DECEMBER){ + my $last_friday; + for my $d (20 .. 31){ + last if($m == FEBRUARY && $d > 28 && !$is_leap_year); + last if($m == FEBRUARY && $d > 29 && $is_leap_year); + last if(($m == APRIL || $m == JUNE || $m == SEPTEMBER || $m == NOVEMBER) && $d > 30); + my $dow = day_of_week(YEAR, $m, $d); + if($dow == FRIDAY){ + $last_friday = $d; + } + } + print YEAR . "/" . sprintf("%02d", $m) . "/$last_friday\n"; +} diff --git a/challenge-013/adam-russell/perl5/ch-2.pl b/challenge-013/adam-russell/perl5/ch-2.pl new file mode 100644 index 0000000000..db8485d41a --- /dev/null +++ b/challenge-013/adam-russell/perl5/ch-2.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +## +# Using mutually recursive methods, generate Hofstadter Female and Male sequences. +## + +sub F{ + my($n) = @_; + if($n == 0){ + return 1; + } + return $n - M(F($n - 1)); +} + +sub M{ + my($n) = @_; + if($n == 0){ + return 0; + } + return $n - F(M($n - 1)); +} + +## +# Main +## +for my $n (0 .. 25){ + print "$n\tM: " . M($n) . "\tF: " . F($n) . "\n"; +} |
