diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-07-01 14:40:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-01 14:40:26 +0100 |
| commit | 92b6c8d4c25b0c037616635ef0dd0d030dbef47e (patch) | |
| tree | 21d2bcfcd1af90094df87df4aeb2a1ebb80d5d4e | |
| parent | aab273335bc065444359cf9765b2475d9501564c (diff) | |
| parent | 2bb854a2d04fe41f25469a84b8b184d266cbba90 (diff) | |
| download | perlweeklychallenge-club-92b6c8d4c25b0c037616635ef0dd0d030dbef47e.tar.gz perlweeklychallenge-club-92b6c8d4c25b0c037616635ef0dd0d030dbef47e.tar.bz2 perlweeklychallenge-club-92b6c8d4c25b0c037616635ef0dd0d030dbef47e.zip | |
Merge pull request #1893 from waltman/branch-for-challenge-067
Branch for challenge 067
| -rw-r--r-- | challenge-067/walt-mankowski/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-067/walt-mankowski/perl/ch-2.pl | 41 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-067/walt-mankowski/perl/ch-1.pl b/challenge-067/walt-mankowski/perl/ch-1.pl new file mode 100644 index 0000000000..4493b84f9e --- /dev/null +++ b/challenge-067/walt-mankowski/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); +use Algorithm::Combinatorics qw(combinations); + +# TASK #1 › Number Combinations +# Submitted by: Mohammad S Anwar +# +# You are given two integers $m and $n. Write a script print all +# possible combinations of $n numbers from the list 1 2 3 … $m. +# +# Every combination should be sorted i.e. [2,3] is valid combination +# but [3,2] is not. Example: +# +# Input: $m = 5, $n = 2 +# +# Output: [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ] +# +# Solution: This is straightforward with combinations() from +# Algorithm::Combinatorics + +my ($m, $n) = @ARGV; + +my @data = 1..$m; +my @combs; +my $iter = combinations(\@data, $n); +while (my $p = $iter->next) { + push @combs, sprintf('[%s]', join(',', $p->@*)); +} +say '[ ', join(", ", @combs), ' ]'; 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); + } + } +} |
