From 8aeb4115265e025381abf10665e92ee0d41411af Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 30 Jun 2020 20:02:42 -0400 Subject: Perl solution to challenge 67 task 1 Algorithm::Combinatorics is doing all the hard work here. --- challenge-067/walt-mankowski/perl/ch-1.pl | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-067/walt-mankowski/perl/ch-1.pl 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), ' ]'; -- cgit From 2bb854a2d04fe41f25469a84b8b184d266cbba90 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 30 Jun 2020 20:33:57 -0400 Subject: Perl solution to challenge 67 task 2 My solution uses recursion to loop over all the possible values --- challenge-067/walt-mankowski/perl/ch-2.pl | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-067/walt-mankowski/perl/ch-2.pl 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); + } + } +} -- cgit