diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-06-18 11:06:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-18 11:06:25 +0100 |
| commit | df6044c6983fad2e8c3812376923f6708e626165 (patch) | |
| tree | f8c796e7acca687652d24a5a3f2c6446d3d342b0 | |
| parent | 7fcc9ab4ea99010e045752c987418515e026d70c (diff) | |
| parent | ca1cbbc75a4ed91b2dcf02c8c0eea6c054aab0bf (diff) | |
| download | perlweeklychallenge-club-df6044c6983fad2e8c3812376923f6708e626165.tar.gz perlweeklychallenge-club-df6044c6983fad2e8c3812376923f6708e626165.tar.bz2 perlweeklychallenge-club-df6044c6983fad2e8c3812376923f6708e626165.zip | |
Merge pull request #275 from gnustavo/013
Gustavo Chaves's solutions to challenge 013
| -rwxr-xr-x | challenge-013/gustavo-chaves/perl5/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-013/gustavo-chaves/perl5/ch-2.pl | 49 |
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-013/gustavo-chaves/perl5/ch-1.pl b/challenge-013/gustavo-chaves/perl5/ch-1.pl new file mode 100755 index 0000000000..1ac67738dd --- /dev/null +++ b/challenge-013/gustavo-chaves/perl5/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# 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 + +use 5.026; +use strict; +use autodie; +use warnings; +use DateTime; + +my $year = shift or die "Usage: $0 YEAR\n"; + +for my $month (1 .. 12) { + my $date = DateTime->last_day_of_month( + year => $year, + month => $month, + time_zone => '+0000', + ); + printf "%4d/%02d/%02d\n", $year, $month, $date->day() - ($date->day_of_week()+2)%7; +} diff --git a/challenge-013/gustavo-chaves/perl5/ch-2.pl b/challenge-013/gustavo-chaves/perl5/ch-2.pl new file mode 100755 index 0000000000..72b6bf1ddd --- /dev/null +++ b/challenge-013/gustavo-chaves/perl5/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +# 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 +# (https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Female_and_Male_sequences). + +use 5.026; +use strict; +use autodie; +use warnings; +use Memoize; + +my $n = shift or die "Usage: $0 N"; + +$n >= 0 or die "N must be non-negative\n"; + +memoize('F'); +sub F { + my ($n) = @_; + if ($n == 0) { + return 1; + } else { + return $n - M(F($n-1)); + } +} + +memoize('M'); +sub M { + my ($n) = @_; + if ($n == 0) { + return 0; + } else { + return $n - F(M($n-1)); + } +} + +print 'F:'; +foreach my $i (0 .. $n) { + print ' ', F($i); +} +print "\n"; + +print 'M:'; +foreach my $i (0 .. $n) { + print ' ', M($i); +} +print "\n"; |
