From 5df7cd60cef6725f8f8c6517af23ccb3d0fbcb4e Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Fri, 3 Jul 2020 18:44:34 +0200 Subject: Solve 067: Number Combinations & Letter Phone by E. Choroba --- challenge-067/e-choroba/perl/ch-1.pl | 19 +++++++++++++++++++ challenge-067/e-choroba/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100755 challenge-067/e-choroba/perl/ch-1.pl create mode 100755 challenge-067/e-choroba/perl/ch-2.pl 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"]; -- cgit