aboutsummaryrefslogtreecommitdiff
path: root/challenge-067
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-29 20:33:04 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-29 20:33:04 +0100
commit0255154e054c650aa6baa34a887083d583c07dfa (patch)
tree978351f4d53ee8c8d45fc2599011c8b8374d614d /challenge-067
parentbfc43d8fd4c801322b5f44b875394f229ec3e177 (diff)
downloadperlweeklychallenge-club-0255154e054c650aa6baa34a887083d583c07dfa.tar.gz
perlweeklychallenge-club-0255154e054c650aa6baa34a887083d583c07dfa.tar.bz2
perlweeklychallenge-club-0255154e054c650aa6baa34a887083d583c07dfa.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-067')
-rw-r--r--challenge-067/ulrich-rieke/perl/ch-1.pl14
-rw-r--r--challenge-067/ulrich-rieke/raku/ch-1.p65
-rw-r--r--challenge-067/ulrich-rieke/raku/ch-2.p661
3 files changed, 80 insertions, 0 deletions
diff --git a/challenge-067/ulrich-rieke/perl/ch-1.pl b/challenge-067/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..aa5db52ccf
--- /dev/null
+++ b/challenge-067/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use Algorithm::Combinatorics qw ( combinations ) ;
+
+my $m = $ARGV[0] ;
+my $n = $ARGV[ 1 ] ;
+my @list = (1..$m) ;
+my $iter = combinations( \@list , $n ) ;
+while ( my $p = $iter->next ) {
+ print "(" ;
+ map { print } @{$p} ;
+ print ")\n" ;
+}
diff --git a/challenge-067/ulrich-rieke/raku/ch-1.p6 b/challenge-067/ulrich-rieke/raku/ch-1.p6
new file mode 100644
index 0000000000..fd7b7bd694
--- /dev/null
+++ b/challenge-067/ulrich-rieke/raku/ch-1.p6
@@ -0,0 +1,5 @@
+use v6 ;
+
+sub MAIN( Int $m , Int $n ) {
+ .say for (1...$m).combinations( $n ) ;
+}
diff --git a/challenge-067/ulrich-rieke/raku/ch-2.p6 b/challenge-067/ulrich-rieke/raku/ch-2.p6
new file mode 100644
index 0000000000..8499e695f1
--- /dev/null
+++ b/challenge-067/ulrich-rieke/raku/ch-2.p6
@@ -0,0 +1,61 @@
+use v6 ;
+
+#We have to provide for the digit 0 with no letters attached
+sub combineTwoStrings( Str $a , Str $b ) {
+ my @combis ;
+ if ( $a && $b ) {
+ for $a.comb -> $firstletter {
+ for $b.comb -> $secondletter {
+ @combis.push( $firstletter ~ $secondletter ) ;
+ }
+ }
+ }
+ if ( $a && not $b ) {
+ @combis.push( $a.comb ) ;
+ }
+ if ( not $a && $b ) {
+ @combis.push( $b.comb ) ;
+ }
+ return @combis ;
+}
+
+#here too, we have to provide for '0' as a possible digit
+sub combineStringWithArray( @array, Str $word ) {
+ my @combis ;
+ if ( $word ) {
+ for $word.comb -> $letter {
+ for @array -> $element {
+ @combis.push( $element ~ $letter) ;
+ }
+ }
+ }
+ else {
+ @combis = @array ;
+ }
+ return @combis ;
+}
+
+sub MAIN( Str $S ) {
+ my %phone = "1" => "-,@" , "2" => "ABC" , "3" => "DEF" , "4" => "GHI",
+ "5" => "JKL" , "6" => "MNO" , "7" => "PQRS" , "8" => "TUV" ,
+ "9" => "WXYZ" , "0" => "" ;
+ my @allCombis ;
+ my $len = $S.chars ;
+ if ( $len == 1 ) {
+ @allCombis.push( %phone{ $S }.comb ) ;
+ }
+ if ( $len == 2 ) {
+ @allCombis = combineTwoStrings( %phone{ $S.substr( 0 , 1 ) } ,
+ %phone{ $S.substr( 1 , 1 ) } ) ;
+ }
+ if ( $len > 2 ) {
+ my @letters = $S.comb ;
+ @allCombis = combineTwoStrings( %phone{ @letters[ 0 ] } ,
+ %phone{ @letters[ 1 ] } ) ;
+ for (2..$len - 1) -> $i {
+ @allCombis = combineStringWithArray( @allCombis ,
+ %phone{ @letters[ $i ] } ) ;
+ }
+ }
+ say @allCombis.map( {.lc} ) ;
+}