diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-06-17 14:44:37 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-06-17 14:44:37 +0100 |
| commit | 0481a80595a156a0de02e531ae66662d711c8b86 (patch) | |
| tree | e346c0456589ee09002d428382a0bf39dd7fd06b /challenge-013 | |
| parent | ce67d818ae31a56a05d1ac9369e2257f84575952 (diff) | |
| download | perlweeklychallenge-club-0481a80595a156a0de02e531ae66662d711c8b86.tar.gz perlweeklychallenge-club-0481a80595a156a0de02e531ae66662d711c8b86.tar.bz2 perlweeklychallenge-club-0481a80595a156a0de02e531ae66662d711c8b86.zip | |
- Added solutions by Kevin Colyer.
Diffstat (limited to 'challenge-013')
| -rw-r--r-- | challenge-013/kevin-colyer/perl6/ch-1.p6 | 14 | ||||
| -rw-r--r-- | challenge-013/kevin-colyer/perl6/ch-2.p6 | 25 |
2 files changed, 39 insertions, 0 deletions
diff --git a/challenge-013/kevin-colyer/perl6/ch-1.p6 b/challenge-013/kevin-colyer/perl6/ch-1.p6 new file mode 100644 index 0000000000..3704c80664 --- /dev/null +++ b/challenge-013/kevin-colyer/perl6/ch-1.p6 @@ -0,0 +1,14 @@ +#!/usr/bin/perl6 +use v6; +use Test; + +# Challenge #13.1 +# Write a script to print the date of last Friday of every month of a given year. Order y, m, d + +my $year=2019; +for 1..12 -> $month { + my $d=Date.new($year,$month,1); + my $dim=$d.days-in-month -1; + my $friday = $d + first {($d+$_).day-of-week==5}, $dim-7..$dim; + say $friday.yyyy-mm-dd; +} diff --git a/challenge-013/kevin-colyer/perl6/ch-2.p6 b/challenge-013/kevin-colyer/perl6/ch-2.p6 new file mode 100644 index 0000000000..87d4deb58f --- /dev/null +++ b/challenge-013/kevin-colyer/perl6/ch-2.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/perl6 +use v6; +use Test; + +# Challenge #2 +# 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. +# +# F ( 0 ) = 1 ; M ( 0 ) = 0 +# F ( n ) = n − M ( F ( n − 1 ) ) , n > 0 +# M ( n ) = n − F ( M ( n − 1 ) ) , n > 0. +# F: 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13, ... (sequence A005378 in the OEIS) +# M: 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 12, ... (sequence A005379 in the OEIS) + +my $f=join ", ", map { F($_) }, (^21); +is $f,"1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13", "Hofstadter Female sequence"; + +my $m=join ", ", map { M($_) }, (^21); +is $m,"0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 12", "Hofstadter Male sequence"; + + +multi F($n where $n==0) { 1 } +multi F($n) { $n - M( F( $n-1 ) ) } + +multi M($n where $n==0) { 0 } +multi M($n) { $n − F( M( $n − 1 ) ) } |
