aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-01 14:51:59 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-01 14:51:59 +0100
commited66e35dea0395be7d16154116c0907d9f4a039e (patch)
tree64b2110346496f973d60432b3a5dcfdaa99a895d
parent7ce3c10c21e237198d35d0e72462f80d570ae099 (diff)
parent92b6c8d4c25b0c037616635ef0dd0d030dbef47e (diff)
downloadperlweeklychallenge-club-ed66e35dea0395be7d16154116c0907d9f4a039e.tar.gz
perlweeklychallenge-club-ed66e35dea0395be7d16154116c0907d9f4a039e.tar.bz2
perlweeklychallenge-club-ed66e35dea0395be7d16154116c0907d9f4a039e.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rw-r--r--challenge-067/walt-mankowski/perl/ch-1.pl32
-rw-r--r--challenge-067/walt-mankowski/perl/ch-2.pl41
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);
+ }
+ }
+}