diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-07-04 14:28:42 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-07-04 14:28:42 +0100 |
| commit | bf3c787d83e8be64c6824de9233615ce46d65e09 (patch) | |
| tree | a13e38182b24aa6446c61ffff5cb0a4e0f240bc8 /challenge-067 | |
| parent | 470ac2eb443f00a44ee1894e90c39635bfe23066 (diff) | |
| download | perlweeklychallenge-club-bf3c787d83e8be64c6824de9233615ce46d65e09.tar.gz perlweeklychallenge-club-bf3c787d83e8be64c6824de9233615ce46d65e09.tar.bz2 perlweeklychallenge-club-bf3c787d83e8be64c6824de9233615ce46d65e09.zip | |
- Added Raku solutions to the Challenge #067.
Diffstat (limited to 'challenge-067')
| -rw-r--r-- | challenge-067/mohammad-anwar/raku/ch-1.raku | 26 | ||||
| -rw-r--r-- | challenge-067/mohammad-anwar/raku/ch-1a.raku | 37 | ||||
| -rw-r--r-- | challenge-067/mohammad-anwar/raku/ch-2.raku | 32 | ||||
| -rw-r--r-- | challenge-067/mohammad-anwar/raku/ch-2a.raku | 40 |
4 files changed, 135 insertions, 0 deletions
diff --git a/challenge-067/mohammad-anwar/raku/ch-1.raku b/challenge-067/mohammad-anwar/raku/ch-1.raku new file mode 100644 index 0000000000..a0fb9c225a --- /dev/null +++ b/challenge-067/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 067 +# +# Task #1: Number Combinations +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-067/ +# + +use v6.d; + +sub MAIN(Int :$m where { $m > 0 } = 5, Int :$n where { $n > 0 } = 2) { + number-combinations($m, $n).say; +} + +sub number-combinations(Int $m where { $m > 0 }, + Int $n where { $n > 0 }) { + $n > $m and say "ERROR: Invalid n=$n (n <= m)" and exit; + + return sprintf("[ %s ]", + (1..$m) + .combinations($n) + .map({ '['~$_.join(',')~']' }) + .join(', ')); +} diff --git a/challenge-067/mohammad-anwar/raku/ch-1a.raku b/challenge-067/mohammad-anwar/raku/ch-1a.raku new file mode 100644 index 0000000000..6694b8d44c --- /dev/null +++ b/challenge-067/mohammad-anwar/raku/ch-1a.raku @@ -0,0 +1,37 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 067 +# +# Task #1: Number Combinations +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-067/ +# + +use Test; + +is number-combinations(5, 2), + '[ [1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5] ]', + 'testing m=5, n=2'; +is number-combinations(5, 3), + '[ [1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5] ]', + 'testing m=5, n=3'; +is number-combinations(5, 4), + '[ [1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5] ]', + 'testing m=5, n=4'; +is number-combinations(5, 5), + '[ [1, 2, 3, 4, 5] ]', + 'testing m=5, n=5'; + +done-testing; + +sub number-combinations(Int $m where { $m > 0 }, + Int $n where { $n > 0 }) { + $n > $m and say "ERROR: Invalid n=$n (n <= m)" and exit; + + return sprintf("[ %s ]", + (1..$m) + .combinations($n) + .map({ '['~$_.join(', ')~']' }) + .join(', ')); +} diff --git a/challenge-067/mohammad-anwar/raku/ch-2.raku b/challenge-067/mohammad-anwar/raku/ch-2.raku new file mode 100644 index 0000000000..0204f3b869 --- /dev/null +++ b/challenge-067/mohammad-anwar/raku/ch-2.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku + +use v6.d; + +sub MAIN(Str :$digits where { $digits ~~ /^ <[0..9\*\#]>+ $/ } = '35') { + say sprintf("[ %s ]", phone-letters($digits).join(", ")); +} + +sub phone-letters(Str $digits where { $digits ~~ /^ <[0..9\*\#]>+ $/ }) { + + # letter phone + my %letter-of = ( + '1' => '_,@', + '2' => 'abc', + '3' => 'def', + '4' => 'ghi', + '5' => 'jlk', + '6' => 'mno', + '7' => 'pqrs', + '8' => 'tuv', + '9' => 'wxyz', + '0' => '0', + '#' => '#', + '*' => chr(9251), + ); + + # prepare data set + my @data = $digits.comb.map({ [ %letter-of{$_}.comb ] }); + + # generate all possible combinations + return map {qq{'$_'}}, ([X~] @data); +} diff --git a/challenge-067/mohammad-anwar/raku/ch-2a.raku b/challenge-067/mohammad-anwar/raku/ch-2a.raku new file mode 100644 index 0000000000..bd28c7ac7d --- /dev/null +++ b/challenge-067/mohammad-anwar/raku/ch-2a.raku @@ -0,0 +1,40 @@ +#!/usr/bin/env raku + +use Test; + +is sprintf("[ %s ]", phone-letters('35').join(", ")), + "[ 'dj', 'dl', 'dk', 'ej', 'el', 'ek', 'fj', 'fl', 'fk' ]", + 'testing "35"'; +is sprintf("[ %s ]", phone-letters('10').join(", ")), + "[ '_0', ',0', '@0' ]", + 'testing "10"'; +is sprintf("[ %s ]", phone-letters('2#').join(", ")), + "[ 'a#', 'b#', 'c#' ]", + 'testing "2#"'; + +done-testing; + +sub phone-letters(Str $digits where { $digits ~~ /^ <[0..9\*\#]>+ $/ }) { + + # letter phone + my %letter-of = ( + '1' => '_,@', + '2' => 'abc', + '3' => 'def', + '4' => 'ghi', + '5' => 'jlk', + '6' => 'mno', + '7' => 'pqrs', + '8' => 'tuv', + '9' => 'wxyz', + '0' => '0', + '#' => '#', + '*' => chr(9251), + ); + + # prepare data set + my @data = $digits.comb.map({ [ %letter-of{$_}.comb ] }); + + # generate all possible combinations + return map {qq{'$_'}}, ([X~] @data); +} |
