Skip to content

Instantly share code, notes, and snippets.

@adamcrussell adamcrussell/ch-1.pl
Created Jun 23, 2019

Embed
What would you like to do?
Perl Weekly Challenge 013
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";
}
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";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.