diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-30 10:49:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-30 10:49:25 +0100 |
| commit | e851a4b4f075c3438f8fcc9cbd6848917274602b (patch) | |
| tree | 26a62d7dbdf86231b32592d789aa17ae05014855 | |
| parent | 1eae9b72da9c927529e8f5f69cd499b824ae9023 (diff) | |
| parent | 13f3a16b6b25953672f49ce56b59c185b6e072fc (diff) | |
| download | perlweeklychallenge-club-e851a4b4f075c3438f8fcc9cbd6848917274602b.tar.gz perlweeklychallenge-club-e851a4b4f075c3438f8fcc9cbd6848917274602b.tar.bz2 perlweeklychallenge-club-e851a4b4f075c3438f8fcc9cbd6848917274602b.zip | |
Merge pull request #1891 from simonminer/pwc-simon-miner-067
PWC 067 solutions by Simon Miner
| -rwxr-xr-x | challenge-067/simon-miner/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-067/simon-miner/perl/ch-2.pl | 45 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-067/simon-miner/perl/ch-1.pl b/challenge-067/simon-miner/perl/ch-1.pl new file mode 100755 index 0000000000..716e8df984 --- /dev/null +++ b/challenge-067/simon-miner/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; + +my ( $m, $n ) = @ARGV; +die "Max number $m is must be an integer.\n" if $m =~ m/\D/; +die "Combination length $n is must be an integer.\n" if $n =~ m/\D/; + +my @combinations = get_combinations( 1, $m, $n - 1 ); +print join( "\n", @combinations ) . "\n"; + +sub get_combinations { + my ( $min, $m, $n ) = @_; + + my @combinations = (); + if ( $n ) { + for my $i ( $min .. ( $m - 1 )) { + for my $combo ( get_combinations( $i + 1, $m, $n - 1 ) ) { + push( @combinations, $i . $combo ); + } + } + } else { + @combinations = ( $min .. $m ); + } + + return @combinations; +} 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; +} |
