diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-06-29 18:55:13 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-07-02 16:39:17 +0200 |
| commit | b094543a0e458253728416a3d01a1c126d2f9327 (patch) | |
| tree | 0aafd3b539e03000b2c51efcda11782d65a5d21f /challenge-067 | |
| parent | 41919f46fd1d0cf0a195cbe19af137c2a938b0e6 (diff) | |
| download | perlweeklychallenge-club-b094543a0e458253728416a3d01a1c126d2f9327.tar.gz perlweeklychallenge-club-b094543a0e458253728416a3d01a1c126d2f9327.tar.bz2 perlweeklychallenge-club-b094543a0e458253728416a3d01a1c126d2f9327.zip | |
solution for task 2
Diffstat (limited to 'challenge-067')
| -rwxr-xr-x | challenge-067/jo-37/perl/ch-2.pl | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/challenge-067/jo-37/perl/ch-2.pl b/challenge-067/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..74b55f4613 --- /dev/null +++ b/challenge-067/jo-37/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use Test2::V0; +use List::Util qw(reduce); + +my %letters = (1 => ['_', ',', '@'], 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)]); + +# Outer product of two arrays of strings: +# Concatenate every element of the left array with every element +# of the right array. +sub prod { + my ($left, $right) = @_; + + [map {my $l = $_; map $l . $_, @$right} @$left]; +} + +# Build the outer product of all dial letters +sub dialstrings { + reduce {prod $a, $letters{$b}} [''], split '', shift; +} + +is dialstrings(35), + ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"], + 'example from challenge'; +ok grep(/^perl_weekly$/, @{dialstrings(73751933559)}), 'perl weekly'; + +done_testing; |
