aboutsummaryrefslogtreecommitdiff
path: root/challenge-067
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2020-07-03 18:44:34 +0200
committerE. Choroba <choroba@matfyz.cz>2020-07-03 18:44:34 +0200
commit5df7cd60cef6725f8f8c6517af23ccb3d0fbcb4e (patch)
tree0d31fa2f6d9b5fdce2f04a438c84bc5e593c3c4c /challenge-067
parent8ea273a1a624ce498a404f854a672a55c48c7d28 (diff)
downloadperlweeklychallenge-club-5df7cd60cef6725f8f8c6517af23ccb3d0fbcb4e.tar.gz
perlweeklychallenge-club-5df7cd60cef6725f8f8c6517af23ccb3d0fbcb4e.tar.bz2
perlweeklychallenge-club-5df7cd60cef6725f8f8c6517af23ccb3d0fbcb4e.zip
Solve 067: Number Combinations & Letter Phone by E. Choroba
Diffstat (limited to 'challenge-067')
-rwxr-xr-xchallenge-067/e-choroba/perl/ch-1.pl19
-rwxr-xr-xchallenge-067/e-choroba/perl/ch-2.pl33
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-067/e-choroba/perl/ch-1.pl b/challenge-067/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..0b5c910812
--- /dev/null
+++ b/challenge-067/e-choroba/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+sub combinations {
+ my ($max, $size) = @_;
+ if ($size == 1) {
+ return map [$_], 1 .. $max
+ } else {
+ return map {
+ my @c = @$_;
+ map [@c, $_], $c[-1] + 1 .. $max
+ } combinations($max, $size - 1)
+ }
+}
+
+use Test::More tests => 1;
+is_deeply [combinations(5, 2)],
+ [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ];
diff --git a/challenge-067/e-choroba/perl/ch-2.pl b/challenge-067/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..219012e1ac
--- /dev/null
+++ b/challenge-067/e-choroba/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+my %keyboard = (2 => [qw[ a b c ]],
+ 3 => [qw[ d e f ]],
+ 4 => [qw[ g h i ]],
+ 5 => [qw[ j k l ]],
+ 6 => [qw[ m n o ]],
+ 7 => [qw[ p q r s ]],
+ 8 => [qw[ t u v ]],
+ 9 => [qw[ w x y z ]]);
+
+sub expand {
+ my (@digits) = @_;
+ if (@digits == 1) {
+ return @{ $keyboard{ $digits[0] } }
+ } else {
+ return map {
+ my $d = $_;
+ map $d . $_, expand(@digits[1 .. $#digits])
+ } @{ $keyboard{ $digits[0] } }
+ }
+}
+
+sub digits2words {
+ my ($number) = @_;
+ return expand(split //, $number)
+}
+
+use Test::More tests => 1;
+is_deeply [digits2words('35')],
+ ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"];