aboutsummaryrefslogtreecommitdiff
path: root/challenge-067
diff options
context:
space:
mode:
authorWalt Mankowski <waltman@pobox.com>2020-06-30 20:33:57 -0400
committerWalt Mankowski <waltman@pobox.com>2020-06-30 20:33:57 -0400
commit2bb854a2d04fe41f25469a84b8b184d266cbba90 (patch)
tree21d2bcfcd1af90094df87df4aeb2a1ebb80d5d4e /challenge-067
parent8aeb4115265e025381abf10665e92ee0d41411af (diff)
downloadperlweeklychallenge-club-2bb854a2d04fe41f25469a84b8b184d266cbba90.tar.gz
perlweeklychallenge-club-2bb854a2d04fe41f25469a84b8b184d266cbba90.tar.bz2
perlweeklychallenge-club-2bb854a2d04fe41f25469a84b8b184d266cbba90.zip
Perl solution to challenge 67 task 2
My solution uses recursion to loop over all the possible values
Diffstat (limited to 'challenge-067')
-rw-r--r--challenge-067/walt-mankowski/perl/ch-2.pl41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-067/walt-mankowski/perl/ch-2.pl b/challenge-067/walt-mankowski/perl/ch-2.pl
new file mode 100644
index 0000000000..de2a6fbb4f
--- /dev/null
+++ b/challenge-067/walt-mankowski/perl/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.30);
+use experimental qw(signatures);
+
+# TASK #2 › Letter Phone
+# Submitted by: Mohammad S Anwar
+#
+# You are given a digit string $S. Write a script to print all
+# possible letter combinations that the given digit string could
+# represent.
+
+# store the letters for each digit as a hash of arraryrefs
+my %key = (1 => [qw(_ @)],
+ 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)],
+ 0 => [' ']
+ );
+
+my $s = $ARGV[0];
+gen_combs($s, 0, '');
+
+# generate the combinations by using recursion to loop over all the
+# possible values
+sub gen_combs($s, $idx, $prefix) {
+ if ($idx == length($s)) {
+ say $prefix;
+ } else {
+ my $digit = substr($s, $idx, 1);
+ for my $c ($key{$digit}->@*) {
+ gen_combs($s, $idx+1, $prefix . $c);
+ }
+ }
+}