aboutsummaryrefslogtreecommitdiff
path: root/challenge-067
diff options
context:
space:
mode:
authorJavier Luque <javier.luque@humanstate.com>2020-07-02 07:07:24 +0100
committerJavier Luque <javier.luque@humanstate.com>2020-07-02 07:07:24 +0100
commit51f6d5083ee3665810116559790cb0b75d2b7cd8 (patch)
tree60b00a2384568daf828bc66dd35dacea5cbb1b55 /challenge-067
parent1332049b96092e73e3cd50d73d87be83e184bf20 (diff)
downloadperlweeklychallenge-club-51f6d5083ee3665810116559790cb0b75d2b7cd8.tar.gz
perlweeklychallenge-club-51f6d5083ee3665810116559790cb0b75d2b7cd8.tar.bz2
perlweeklychallenge-club-51f6d5083ee3665810116559790cb0b75d2b7cd8.zip
Solutions for javier luque challenge 67
Diffstat (limited to 'challenge-067')
-rw-r--r--challenge-067/javier-luque/blog.txt1
-rw-r--r--challenge-067/javier-luque/ch-1.p67
-rw-r--r--challenge-067/javier-luque/ch-1.pl37
-rw-r--r--challenge-067/javier-luque/ch-2.p644
-rw-r--r--challenge-067/javier-luque/ch-2.pl54
5 files changed, 143 insertions, 0 deletions
diff --git a/challenge-067/javier-luque/blog.txt b/challenge-067/javier-luque/blog.txt
new file mode 100644
index 0000000000..debf5b6e2d
--- /dev/null
+++ b/challenge-067/javier-luque/blog.txt
@@ -0,0 +1 @@
+https://perlchallenges.wordpress.com/2020/06/29/perl-weekly-challenge-067/
diff --git a/challenge-067/javier-luque/ch-1.p6 b/challenge-067/javier-luque/ch-1.p6
new file mode 100644
index 0000000000..f621469e2f
--- /dev/null
+++ b/challenge-067/javier-luque/ch-1.p6
@@ -0,0 +1,7 @@
+# Test: perl6 ch-1.p6
+multi MAIN() { MAIN(5, 2); }
+
+multi MAIN(Int $m, Int $n) {
+ my @data = (1 .. $m);
+ say @data.combinations: $n;
+}
diff --git a/challenge-067/javier-luque/ch-1.pl b/challenge-067/javier-luque/ch-1.pl
new file mode 100644
index 0000000000..bc1da22c8e
--- /dev/null
+++ b/challenge-067/javier-luque/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+# Test: ./ch-1.pl
+use Modern::Perl;
+use Algorithm::Combinatorics qw(combinations);
+
+# Default $m and $n
+my $m = shift // 5;
+my $n = shift // 2;
+
+# Answers
+my @combinations = numeric_combinations($m, $n);
+say answer_to_string(\@combinations);
+
+sub numeric_combinations {
+ my ($m, $n) = @_;
+ my @data = (1 .. $m);
+
+ # Possible combinations
+ return my @all_combinations
+ = combinations(\@data, $n);
+}
+
+# Flaten to answer array to a string
+sub answer_to_string {
+ my $combinations = shift;
+ return
+ '[ ' .
+ (
+ join ', ',
+ map {
+ '[' .
+ (join ', ', @$_) .
+ ']'
+ } @$combinations
+ ) .
+ ' ]';
+}
diff --git a/challenge-067/javier-luque/ch-2.p6 b/challenge-067/javier-luque/ch-2.p6
new file mode 100644
index 0000000000..df26a55010
--- /dev/null
+++ b/challenge-067/javier-luque/ch-2.p6
@@ -0,0 +1,44 @@
+# Test: perl6 ch-1.p6
+
+# Phone key transations
+my %phone_keys = (
+ '1' => ['_', ',', '@'],
+ '2' => ['a', 'b', 'c'],
+ '3' => ['d', 'e', 'f'],
+ '4' => ['g', 'h', 'i'],
+ '5' => ['j', 'k', 'l'],
+ '6' => ['m', 'n', 'o'],
+ '7' => ['p', 'q', 'r', 's'],
+ '8' => ['t', 'u', 'v'],
+ '9' => ['w', 'x', 'y', 'z'],
+ '*' => ['_'],
+ '0' => [''],
+ '#' => [''],
+);
+
+multi MAIN() { MAIN('35'); }
+
+multi MAIN(Str $S) {
+ # Output the answer
+ say combos($S).perl;
+}
+
+# Generate the possible combinations
+sub combos(Str $S) {
+ my @answers;
+
+ my $letter = $S.substr(0, 1);
+ my $rest_of_word = $S.substr(1);
+
+ for (@(%phone_keys{$letter})) -> $l {
+ if ($rest_of_word) {
+ my @partial_answers =
+ combos($rest_of_word);
+ @answers.append(@partial_answers.map({ $l ~ $_ }));
+ } else {
+ @answers.append($l);
+ }
+ }
+
+ return @answers;
+}
diff --git a/challenge-067/javier-luque/ch-2.pl b/challenge-067/javier-luque/ch-2.pl
new file mode 100644
index 0000000000..6252f65c13
--- /dev/null
+++ b/challenge-067/javier-luque/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+# Test: ./ch-2.pl
+use Modern::Perl;
+
+# Default $m and $n
+my $S = shift // '35';
+
+# Phone key transations
+my $phone_keys = {
+ '1' => ['_', ',', '@'],
+ '2' => ['a', 'b', 'c'],
+ '3' => ['d', 'e', 'f'],
+ '4' => ['g', 'h', 'i'],
+ '5' => ['j', 'k', 'l'],
+ '6' => ['m', 'n', 'o'],
+ '7' => ['p', 'q', 'r', 's'],
+ '8' => ['t', 'u', 'v'],
+ '9' => ['w', 'x', 'y', 'z'],
+ '*' => ['_'],
+ '0' => [''],
+ '#' => [''],
+};
+
+# Output the answer
+say
+ '[ "' .
+ ( join '", "',
+ combos($S)
+ ) .
+ '" ]';
+
+# Generate the possible combinations
+sub combos {
+ my $S = shift;
+ my @answers;
+
+ my ($letter, $rest_of_word) =
+ split('',$S,2);
+
+ for my $l (@{$phone_keys->{$letter}}) {
+ if ($rest_of_word) {
+ my @partial_answers =
+ combos($rest_of_word);
+
+ push @answers,
+ map { $l . $_}
+ @partial_answers;
+ } else {
+ push @answers, $l;
+ }
+ }
+
+ return @answers;
+}