diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-06-22 08:27:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-22 08:27:31 +0100 |
| commit | 89c2aabee6f39eca677ed71dfdce3ffb4d429a5c (patch) | |
| tree | c884309e12ff0df4a281cd32382843362f04afdf | |
| parent | 43a620d6b04d98e51d7b3ddd169acfbc6d1164dc (diff) | |
| parent | 3c5f757e9c013c6d15aeb30620e915462a554c2b (diff) | |
| download | perlweeklychallenge-club-89c2aabee6f39eca677ed71dfdce3ffb4d429a5c.tar.gz perlweeklychallenge-club-89c2aabee6f39eca677ed71dfdce3ffb4d429a5c.tar.bz2 perlweeklychallenge-club-89c2aabee6f39eca677ed71dfdce3ffb4d429a5c.zip | |
Merge pull request #283 from choroba/choroba13
Add solutions to 013 by e-choroba
| -rwxr-xr-x | challenge-013/e-choroba/perl5/ch-1.pl | 43 | ||||
| -rwxr-xr-x | challenge-013/e-choroba/perl5/ch-2.pl | 31 |
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-013/e-choroba/perl5/ch-1.pl b/challenge-013/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..ce212adbb2 --- /dev/null +++ b/challenge-013/e-choroba/perl5/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Time::Piece; +use Time::Seconds; + +sub last_fridays { + my ($year) = @_; + my @fridays = map _last_friday($year, $_), 2 .. 12; + push @fridays, _last_friday($year + 1, 1); + return @fridays +} + +sub _last_friday { + my ($year, $month) = @_; + my $date = 'Time::Piece'->strptime("$year/$month/1", '%Y/%m/%d'); + my $diff = ($date->wday + 1) % 7 || 7; + $date = $date - $diff * ONE_DAY; + return $date->strftime('%Y/%m/%d'); +} + +use Test::More tests => 1; + +my @list = last_fridays(2019); +warn "@list"; +chomp( my @expected = <DATA> ); + +is_deeply \@list, \@expected; + +__DATA__ +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 diff --git a/challenge-013/e-choroba/perl5/ch-2.pl b/challenge-013/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..58c76c320d --- /dev/null +++ b/challenge-013/e-choroba/perl5/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Function::Parameters; + +use Memoize; +memoize('F'); +memoize('M'); + +fun F ($n) { + return $n ? $n - M(F($n - 1)) : 1 +} + +fun M ($n) { + return $n ? $n - F(M($n - 1)) : 0 +} + +use Test::More; + +warn +(F(98))[-1]; # To see the efficiency + +is_deeply + [map F($_), 0 .. 20], + [1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13]; + +is_deeply + [map M($_), 0 .. 20], + [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 12]; + +done_testing(); |
