aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-013/yozen-hernandez/blog.txt1
-rwxr-xr-xchallenge-013/yozen-hernandez/perl5/ch-1.pl47
-rwxr-xr-xchallenge-013/yozen-hernandez/perl5/ch-2.pl32
3 files changed, 80 insertions, 0 deletions
diff --git a/challenge-013/yozen-hernandez/blog.txt b/challenge-013/yozen-hernandez/blog.txt
new file mode 100644
index 0000000000..53f84ddd40
--- /dev/null
+++ b/challenge-013/yozen-hernandez/blog.txt
@@ -0,0 +1 @@
+https://yzhernand.github.io/posts/perl-weekly-challenge-13/
diff --git a/challenge-013/yozen-hernandez/perl5/ch-1.pl b/challenge-013/yozen-hernandez/perl5/ch-1.pl
new file mode 100755
index 0000000000..ce2673c7a2
--- /dev/null
+++ b/challenge-013/yozen-hernandez/perl5/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw(say state);
+use DateTime;
+use DateTime::Duration;
+
+# Write a script to print the date of last Friday of every month of a given
+# year. For example, if the given year is 2019 then it should print the
+# following:
+#
+# 2019/01/25
+# 2019/02/22
+# 2019/03/29
+# 2019/04/26
+# 2019/05/31
+# 2019/06/28
+# 2019/07/26
+# 2019/08/30
+# 2019/09/27
+# 2019/10/25
+# 2019/11/29
+# 2019/12/27
+
+my $year = shift or die "Usage: $0 <year>\n";
+
+my $dt = DateTime->new(
+ month => 1,
+ day => 1,
+ year => $year
+);
+my $add_week = DateTime::Duration->new( weeks => 1 );
+my $add_day = DateTime::Duration->new( days => 1 );
+
+while ( $dt->day_of_week() != 5 ) { $dt->add($add_day); }
+
+my %last_fri;
+while ( $dt->year == $year ) {
+ $last_fri{ $dt->month } = $dt->ymd("/");
+ $dt->add($add_week);
+}
+
+{
+ local $, = "\n";
+ say @last_fri{ sort { $a <=> $b } keys %last_fri };
+}
diff --git a/challenge-013/yozen-hernandez/perl5/ch-2.pl b/challenge-013/yozen-hernandez/perl5/ch-2.pl
new file mode 100755
index 0000000000..dcb92bb32b
--- /dev/null
+++ b/challenge-013/yozen-hernandez/perl5/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw(say state);
+use Carp;
+
+# Write a script to demonstrate Mutually Recursive methods. Two methods are
+# mutually recursive if the first method calls the second and the second calls
+# first in turn. Using the mutually recursive methods, generate Hofstadter
+# Female and Male sequences.
+
+sub hofstadter_F {
+ my $n = shift;
+ state %cache = ( 0 => 1 );
+
+ return $cache{$n} if exists $cache{$n};
+ return $cache{$n} = $n - hofstadter_M( hofstadter_F( $n - 1 ) );
+}
+
+sub hofstadter_M {
+ my $n = shift;
+ state %cache = ( 0 => 0 );
+
+ return $cache{$n} if exists $cache{$n};
+ return $cache{$n} = $n - hofstadter_F( hofstadter_M( $n - 1 ) );
+}
+
+my $n = shift or die "Usage: $0 <n>\n";
+
+say "F: ", join( ", ", map { hofstadter_F($_) } ( 0 .. $n ) );
+say "M: ", join( ", ", map { hofstadter_M($_) } ( 0 .. $n ) );