diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-19 11:05:07 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-19 11:05:07 +0100 |
| commit | 81df774812960f71c2e410be594b7586cc8d3a01 (patch) | |
| tree | 0e43ba2f42ec6c1f9417972edee19692cacb7fb4 | |
| parent | 30ac1701a85634273859dbb28f7858bddf939a8b (diff) | |
| download | perlweeklychallenge-club-81df774812960f71c2e410be594b7586cc8d3a01.tar.gz perlweeklychallenge-club-81df774812960f71c2e410be594b7586cc8d3a01.tar.bz2 perlweeklychallenge-club-81df774812960f71c2e410be594b7586cc8d3a01.zip | |
Add Perl solution to challenge 067
| -rw-r--r-- | challenge-067/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-067/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-067/paulo-custodio/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-067/paulo-custodio/perl/ch-2.pl | 48 | ||||
| -rw-r--r-- | challenge-067/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-067/paulo-custodio/t/test-2.yaml | 5 |
6 files changed, 88 insertions, 0 deletions
diff --git a/challenge-067/paulo-custodio/Makefile b/challenge-067/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-067/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-067/paulo-custodio/README b/challenge-067/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-067/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-067/paulo-custodio/perl/ch-1.pl b/challenge-067/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..2a0587b5df --- /dev/null +++ b/challenge-067/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +# Challenge 067 +# +# 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] ] + +use Modern::Perl; +use Math::Combinatorics 'combine'; + +my($m, $n) = @ARGV; +my @m = (1..$m); +my %out; +for (combine($n, @m)) { + $out{"[".join(",", sort @$_)."]"} = 1; +} +say "[ ", join(", ", sort keys %out), " ]"; diff --git a/challenge-067/paulo-custodio/perl/ch-2.pl b/challenge-067/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..bd12104b1f --- /dev/null +++ b/challenge-067/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +# Challenge 067 +# +# 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. +# +# Letter Phone +# +# +# Example: +# Input: $S = '35' +# +# Output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"]. + +use Modern::Perl; +use Iterator::Array::Jagged; + +my %digits = ( + 1 => ['_', ',', '@'], + 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 )], + '*' => [' '], +); + +my $s = shift||""; +my @out = letter_phone($s); +say "[", join(", ", map {'"'.$_.'"'} @out), "]"; + +sub letter_phone { + my($s) = @_; + my @s = map {$digits{$_}} split(//, $s); + my $it = Iterator::Array::Jagged->new(data => \@s); + my @out; + while (my @set = $it->next) { + push @out, join("", @set); + } + return sort @out; +} diff --git a/challenge-067/paulo-custodio/t/test-1.yaml b/challenge-067/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..4ed336268c --- /dev/null +++ b/challenge-067/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 5 2 + input: + output: [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ] diff --git a/challenge-067/paulo-custodio/t/test-2.yaml b/challenge-067/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..699383fa69 --- /dev/null +++ b/challenge-067/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 35 + input: + output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"] |
