diff options
| author | Simon Miner <simon.miner@gmail.com> | 2020-06-29 22:50:08 -0400 |
|---|---|---|
| committer | Simon Miner <simon.miner@gmail.com> | 2020-06-29 22:50:08 -0400 |
| commit | 13f3a16b6b25953672f49ce56b59c185b6e072fc (patch) | |
| tree | 26a62d7dbdf86231b32592d789aa17ae05014855 /challenge-067 | |
| parent | 70552fa3c55dd81add1362217a0c448505e4ba7b (diff) | |
| download | perlweeklychallenge-club-13f3a16b6b25953672f49ce56b59c185b6e072fc.tar.gz perlweeklychallenge-club-13f3a16b6b25953672f49ce56b59c185b6e072fc.tar.bz2 perlweeklychallenge-club-13f3a16b6b25953672f49ce56b59c185b6e072fc.zip | |
Add ch-2.pl for letter phone task
Diffstat (limited to 'challenge-067')
| -rwxr-xr-x | challenge-067/simon-miner/perl/ch-2.pl | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-067/simon-miner/perl/ch-2.pl b/challenge-067/simon-miner/perl/ch-2.pl new file mode 100755 index 0000000000..b3856a6dc8 --- /dev/null +++ b/challenge-067/simon-miner/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; + +my @letters = ( + ['+'], # Need some way to track 0 to avoid colliding strings. + [ '_', ',', '@' ], + [qw( A B C )], + [qw( D E F )], + [qw( G H I )], + [qw( J K L )], + [qw( M N O )], + [qw( P Q R S )], + [qw( T U V )], + [qw( W X Y Z )], +); + +my $S = shift( @ARGV ); +die "Stringcan only contain digits 0-9" if $S =~ m/[\D]/; + +my @letter_strings = get_letter_strings( $S ); +print join( "\n", @letter_strings ) . "\n"; + +sub get_letter_strings { + my ( $S ) = @_; + return () unless $S; + + $S =~ s/^(\d)// && ( my $digit = $1 ); + + my @letter_strings = (); + my @letters = @{ $letters[$digit] }; + if ( $S ) { + for my $letter ( @letters ) { + for my $letter_string ( get_letter_strings( $S ) ) { + push( @letter_strings, $letter . $letter_string ); + } + } + } + else { + @letter_strings = @letters; + } + + return @letter_strings; +} |
