diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-07-04 11:06:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-04 11:06:28 +0100 |
| commit | e11d729d697e6e1fdaadb656b88db10b7fe66693 (patch) | |
| tree | ce152762ef1ded8e0519c6a5fd59abbe0d29ea01 | |
| parent | 18913f14c20d57183f3cbf365d5fcde4e6cfb3a7 (diff) | |
| parent | 18597472f9e0d11c68465ac5751d42a6c890bb0d (diff) | |
| download | perlweeklychallenge-club-e11d729d697e6e1fdaadb656b88db10b7fe66693.tar.gz perlweeklychallenge-club-e11d729d697e6e1fdaadb656b88db10b7fe66693.tar.bz2 perlweeklychallenge-club-e11d729d697e6e1fdaadb656b88db10b7fe66693.zip | |
Merge pull request #1899 from PerlMonk-Athanasius/branch-for-challenge-067
Perl & Raku solutions to Tasks 1 & 2 of the Perl Weekly Challenge #067
| -rw-r--r-- | challenge-067/athanasius/perl/ch-1.pl | 100 | ||||
| -rw-r--r-- | challenge-067/athanasius/perl/ch-2.pl | 139 | ||||
| -rw-r--r-- | challenge-067/athanasius/raku/ch-1.raku | 74 | ||||
| -rw-r--r-- | challenge-067/athanasius/raku/ch-2.raku | 131 |
4 files changed, 444 insertions, 0 deletions
diff --git a/challenge-067/athanasius/perl/ch-1.pl b/challenge-067/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..10b71a7f60 --- /dev/null +++ b/challenge-067/athanasius/perl/ch-1.pl @@ -0,0 +1,100 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 067 +========================= + +Task #1 +------- +*Number Combinations* + +*Submitted by:* Mohammad S Anwar + +You are given two integers $m and $n. Write a script [to] 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] ] + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +use strict; +use warnings; +use Algorithm::Loops qw( NestedLoops ); +use Const::Fast; +use Regexp::Common qw( number ); + +const my $USAGE => +"Usage: + perl $0 <m> <n> + + <m> Integer > 0: maximum of numbers to select + <n> Integer > 0: size of each n-combination\n"; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + print "Challenge 067, Task #1: Number Combinations (Perl)\n\n"; + + my ($m, $n) = parse_command_line(); + my @combs; + + NestedLoops + ( + [ + [ 1 .. $m ], # Outer loop + (sub { [ $_ + 1 .. $m ] }) x ($n - 1), # Inner loops + ], + + sub # Record one combination + { + push @combs, sprintf '[%s]', join ',', @_; + }, + ); + + my $count = scalar @combs; + + printf "There %s %d unique %d-combination%s of the integers 1 to %d:\n[ " . + "%s ]\n", $count == 1 ? 'is' : 'are', $count, $n, + $count == 1 ? '' : 's', $m, join ', ', @combs; +} + +#------------------------------------------------------------------------------- +sub parse_command_line +#------------------------------------------------------------------------------- +{ + scalar @ARGV == 2 or die $USAGE; + + my ($m, $n) = @ARGV; + + $m =~ / \A $RE{num}{int} \z /x && $m > 0 or die $USAGE; + $n =~ / \A $RE{num}{int} \z /x && $n > 0 or die $USAGE; + + $n <= $m or warn "Note: No combinations are possible, because n > m\n"; + + return ($m, $n); +} + +################################################################################ diff --git a/challenge-067/athanasius/perl/ch-2.pl b/challenge-067/athanasius/perl/ch-2.pl new file mode 100644 index 0000000000..f650c9577f --- /dev/null +++ b/challenge-067/athanasius/perl/ch-2.pl @@ -0,0 +1,139 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly 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> + +╭────────────────────────────────────╮ +⎪ ╭────────╮ ╭────────╮ ╭────────╮ ⎪ +⎪ ⎪ 1 _,@ ⎪ ⎪ 2 ABC ⎪ ⎪ 3 DEF ⎪ ⎪ +⎪ ╰────────╯ ╰────────╯ ╰────────╯ ⎪ +⎪ ╭────────╮ ╭────────╮ ╭────────╮ ⎪ +⎪ ⎪ 4 GHI ⎪ ⎪ 5 JKL ⎪ ⎪ 6 MNO ⎪ ⎪ +⎪ ╰────────╯ ╰────────╯ ╰────────╯ ⎪ +⎪ ╭────────╮ ╭────────╮ ╭────────╮ ⎪ +⎪ ⎪ 7 PQRS ⎪ ⎪ 8 TUV ⎪ ⎪ 9 WXYZ ⎪ ⎪ +⎪ ╰────────╯ ╰────────╯ ╰────────╯ ⎪ +⎪ ╭────────╮ ╭────────╮ ╭────────╮ ⎪ +⎪ ⎪ * ⸤_⸥ ⎪ ⎪ 0 ⎪ ⎪ # ⎪ ⎪ +⎪ ╰────────╯ ╰────────╯ ╰────────╯ ⎪ +╰────────────────────────────────────╯ + +*Example:* + + Input: $S = '35' + + Output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"]. + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +#******************************************************************************* +# Assumptions/interpretations: +# +# (1) "Letters" include the characters corresponding to keys "1" and "*" +# (2) The characters corresponding to key "1" are: underscore, comma, and the +# "at" symbol +# (3) The character corresponding to the key "*" is a (single) space +# (4) The "0" and "#" keys are allowed as "digits" in the input digit string, +# but -- as no characters correspond to them -- they are ignored in produc- +# ing the possible output letter combinations +#******************************************************************************* + +use strict; +use warnings; +use Algorithm::Loops qw( NestedLoops ); +use Const::Fast; + +const my $USAGE => +"Usage: + perl $0 <S> + + <S> String of phone digits (0..9, *, #)\n"; + +const my %LETTERS => +( + 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 ) ], + '*' => [ ' ' ], + 0 => [ ], + '#' => [ ], +); + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + print "Challenge 067, Task #2: Letter Phone (Perl)\n\n"; + + my $digits = parse_command_line(); + my @loops; + + for my $digit (@$digits) + { + my @letters = $LETTERS{ $digit }->@*; + push @loops, \@letters if scalar @letters > 0; + } + + my @combinations; + + NestedLoops + ( + \@loops, + sub + { + push @combinations, sprintf '"%s"', join '', map { lc $_ } @_; + }, + ); + + printf "[%s]\n", join ', ', @combinations; +} + +#------------------------------------------------------------------------------- +sub parse_command_line +#------------------------------------------------------------------------------- +{ + scalar @ARGV == 1 or die $USAGE; + + my @digits = split '', $ARGV[0]; + + scalar @digits > 0 or die $USAGE; + + exists $LETTERS{$_} or die $USAGE for @digits; + + return \@digits; +} + +################################################################################ diff --git a/challenge-067/athanasius/raku/ch-1.raku b/challenge-067/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..672c95ce0f --- /dev/null +++ b/challenge-067/athanasius/raku/ch-1.raku @@ -0,0 +1,74 @@ +use v6d; + +################################################################################ +=begin comment + +Perl Weekly Challenge 067 +========================= + +Task #1 +------- +*Number Combinations* + +*Submitted by:* Mohammad S Anwar + +You are given two integers $m and $n. Write a script [to] 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] ] + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +#------------------------------------------------------------------------------- +BEGIN ''.put; +#------------------------------------------------------------------------------- + +#=============================================================================== +sub MAIN +( + UInt:D $m where { $m > 0 }, #= Integer > 0: maximum of numbers to select + UInt:D $n where { $n > 0 }, #= Integer > 0: size of each n-combination +) +#=============================================================================== +{ + "Challenge 067, Task #1: Number Combinations (Raku)\n".put; + + 'Note: No combinations are possible, because n > m'.note if $n > $m; + + my Str @combs; + + for (1 .. $m).combinations: $n -> List $comb + { + @combs.push: '[%s]'.sprintf: $comb.join: ','; + } + + my UInt $count = @combs.elems; + + "There %s %d unique %d-combination%s of the integers 1 to %d:\n[ %s ]\n". + printf: $count == 1 ?? 'is' !! 'are', $count, $n, + $count == 1 ?? '' !! 's', $m, @combs.join: ', '; +} + +#------------------------------------------------------------------------------- +sub USAGE() +#------------------------------------------------------------------------------- +{ + my Str $usage = $*USAGE; + + $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/; + $usage.put; +} + +################################################################################ diff --git a/challenge-067/athanasius/raku/ch-2.raku b/challenge-067/athanasius/raku/ch-2.raku new file mode 100644 index 0000000000..f47e68f348 --- /dev/null +++ b/challenge-067/athanasius/raku/ch-2.raku @@ -0,0 +1,131 @@ +use v6d; + +################################################################################ +=begin comment + +Perl Weekly 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> + +╭────────────────────────────────────╮ +⎪ ╭────────╮ ╭────────╮ ╭────────╮ ⎪ +⎪ ⎪ 1 _,@ ⎪ ⎪ 2 ABC ⎪ ⎪ 3 DEF ⎪ ⎪ +⎪ ╰────────╯ ╰────────╯ ╰────────╯ ⎪ +⎪ ╭────────╮ ╭────────╮ ╭────────╮ ⎪ +⎪ ⎪ 4 GHI ⎪ ⎪ 5 JKL ⎪ ⎪ 6 MNO ⎪ ⎪ +⎪ ╰────────╯ ╰────────╯ ╰────────╯ ⎪ +⎪ ╭────────╮ ╭────────╮ ╭────────╮ ⎪ +⎪ ⎪ 7 PQRS ⎪ ⎪ 8 TUV ⎪ ⎪ 9 WXYZ ⎪ ⎪ +⎪ ╰────────╯ ╰────────╯ ╰────────╯ ⎪ +⎪ ╭────────╮ ╭────────╮ ╭────────╮ ⎪ +⎪ ⎪ * ⸤_⸥ ⎪ ⎪ 0 ⎪ ⎪ # ⎪ ⎪ +⎪ ╰────────╯ ╰────────╯ ╰────────╯ ⎪ +╰────────────────────────────────────╯ + +*Example:* + + Input: $S = '35' + + Output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"]. + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +#******************************************************************************* +# Assumptions/interpretations: +# +# (1) "Letters" include the characters corresponding to keys "1" and "*" +# (2) The characters corresponding to key "1" are: underscore, comma, and the +# "at" symbol +# (3) The character corresponding to the key "*" is a (single) space +# (4) The "0" and "#" keys are allowed as "digits" in the input digit string, +# but -- as no characters correspond to them -- they are ignored in produc- +# ing the possible output letter combinations +#******************************************************************************* + +subset DigitStr of Str where * ~~ / ^ <[ 0 .. 9 * # ]>+ $ /; + +constant %LETTERS = +{ + 1 => [ < _ , @ > ], + 2 => [ < A B C > ], + 3 => [ < D E F > ], + 4 => [ < G H I > ], + 5 => [ < J K L > ], + 6 => [ < M N O > ], + 7 => [ < P Q R S > ], + 8 => [ < T U V > ], + 9 => [ < W X Y Z > ], + '*' => [ ' ' ], + 0 => [ ], + '#' => [ ], +}; + +#------------------------------------------------------------------------------- +BEGIN ''.put; +#------------------------------------------------------------------------------- + +#=============================================================================== +sub MAIN +( + DigitStr:D $S, #= String of phone digits (0..9, *, #) +) +#=============================================================================== +{ + "Challenge 067, Task #2: Letter Phone (Raku)\n".put; + + my DigitStr @digits = $S.split: '', :skip-empty; + my Str @combinations; + + for @digits -> DigitStr $digit + { + my Str @letters = %LETTERS{ $digit }.list; + + if @letters.elems > 0 + { + if @combinations.elems == 0 + { + @combinations.push: .lc for @letters; + } + else + { + my Str @temp-array; + + for @combinations -> Str $combination + { + @temp-array.push: $combination ~ .lc for @letters; + } + + @combinations = @temp-array; + } + } + } + + "[%s]\n".printf: @combinations.map({ qq["$_"] }).join: ', '; +} + +#------------------------------------------------------------------------------- +sub USAGE() +#------------------------------------------------------------------------------- +{ + my Str $usage = $*USAGE; + + $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/; + $usage.put; +} + +################################################################################ |
