diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-07-01 23:08:31 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-07-01 23:08:31 +0100 |
| commit | 1332049b96092e73e3cd50d73d87be83e184bf20 (patch) | |
| tree | b63c3a481fb481e967280e152be01b0c17ff6c10 /challenge-067 | |
| parent | 56ffaee884c72324292ec6d3e69b4ee6708dbeae (diff) | |
| download | perlweeklychallenge-club-1332049b96092e73e3cd50d73d87be83e184bf20.tar.gz perlweeklychallenge-club-1332049b96092e73e3cd50d73d87be83e184bf20.tar.bz2 perlweeklychallenge-club-1332049b96092e73e3cd50d73d87be83e184bf20.zip | |
- Added solutions by Arne Sommer.
Diffstat (limited to 'challenge-067')
| -rw-r--r-- | challenge-067/arne-sommer/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-067/arne-sommer/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-067/arne-sommer/perl/ch-2.pl | 46 | ||||
| -rwxr-xr-x | challenge-067/arne-sommer/perl/letterphone-perl | 46 | ||||
| -rwxr-xr-x | challenge-067/arne-sommer/perl/numcom-perl | 25 | ||||
| -rwxr-xr-x | challenge-067/arne-sommer/raku/ch-1.raku | 5 | ||||
| -rwxr-xr-x | challenge-067/arne-sommer/raku/ch-2.raku | 41 | ||||
| -rwxr-xr-x | challenge-067/arne-sommer/raku/letterphone | 41 | ||||
| -rwxr-xr-x | challenge-067/arne-sommer/raku/numcom | 5 |
9 files changed, 235 insertions, 0 deletions
diff --git a/challenge-067/arne-sommer/blog.txt b/challenge-067/arne-sommer/blog.txt new file mode 100644 index 0000000000..3706b44247 --- /dev/null +++ b/challenge-067/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/numbers-letters.html diff --git a/challenge-067/arne-sommer/perl/ch-1.pl b/challenge-067/arne-sommer/perl/ch-1.pl new file mode 100755 index 0000000000..21a7c2f201 --- /dev/null +++ b/challenge-067/arne-sommer/perl/ch-1.pl @@ -0,0 +1,25 @@ +#! /usr/bin/env perl + +use Algorithm::Combinatorics qw(combinations); +use feature 'say'; + +my $m = shift(@ARGV) || die 'Please specify $m and $n'; +my $n = shift(@ARGV) || die 'Please specify $n'; + +die "XX" unless int($m) == $m; +die "XX" unless int($n) == $n; + +die "XX" unless $m > 0; +die "XX" unless $n > 0; + +my @numbers = 1 .. $m; + +my @answer; + +my $iter = combinations(\@numbers, $n); +while (my $c = $iter->next) +{ + push(@answer, "[" . join(",", @{$c}) . "]"); +} + +say "[ ", join(", ", @answer), " ]"; diff --git a/challenge-067/arne-sommer/perl/ch-2.pl b/challenge-067/arne-sommer/perl/ch-2.pl new file mode 100755 index 0000000000..e8428e3104 --- /dev/null +++ b/challenge-067/arne-sommer/perl/ch-2.pl @@ -0,0 +1,46 @@ +#! /usr/bin/env perl + +use feature 'say'; +use feature 'signatures'; +no warnings qw(experimental::signatures); + +my $S = shift(@ARGV) || die 'Specify $S'; + +my %button = +( + '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'], + '*' => [ ' '] +); + +my @solutions; + +off_we_go("", $S); + +say "[", join(", ", map { "\"$_\"" } @solutions), "]" if @solutions; + +sub off_we_go ($so_far, $to_do) +{ + if (length($to_do) == 0) + { + push(@solutions, $so_far); + return; + } + + my $current = substr($to_do, 0,1); + my $remainder = substr($to_do, 1); + + die "Illegal character $current" unless $button{$current}; + + for my $character (@{$button{$current}}) + { + off_we_go($so_far . $character, $remainder); + } +} diff --git a/challenge-067/arne-sommer/perl/letterphone-perl b/challenge-067/arne-sommer/perl/letterphone-perl new file mode 100755 index 0000000000..e8428e3104 --- /dev/null +++ b/challenge-067/arne-sommer/perl/letterphone-perl @@ -0,0 +1,46 @@ +#! /usr/bin/env perl + +use feature 'say'; +use feature 'signatures'; +no warnings qw(experimental::signatures); + +my $S = shift(@ARGV) || die 'Specify $S'; + +my %button = +( + '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'], + '*' => [ ' '] +); + +my @solutions; + +off_we_go("", $S); + +say "[", join(", ", map { "\"$_\"" } @solutions), "]" if @solutions; + +sub off_we_go ($so_far, $to_do) +{ + if (length($to_do) == 0) + { + push(@solutions, $so_far); + return; + } + + my $current = substr($to_do, 0,1); + my $remainder = substr($to_do, 1); + + die "Illegal character $current" unless $button{$current}; + + for my $character (@{$button{$current}}) + { + off_we_go($so_far . $character, $remainder); + } +} diff --git a/challenge-067/arne-sommer/perl/numcom-perl b/challenge-067/arne-sommer/perl/numcom-perl new file mode 100755 index 0000000000..21a7c2f201 --- /dev/null +++ b/challenge-067/arne-sommer/perl/numcom-perl @@ -0,0 +1,25 @@ +#! /usr/bin/env perl + +use Algorithm::Combinatorics qw(combinations); +use feature 'say'; + +my $m = shift(@ARGV) || die 'Please specify $m and $n'; +my $n = shift(@ARGV) || die 'Please specify $n'; + +die "XX" unless int($m) == $m; +die "XX" unless int($n) == $n; + +die "XX" unless $m > 0; +die "XX" unless $n > 0; + +my @numbers = 1 .. $m; + +my @answer; + +my $iter = combinations(\@numbers, $n); +while (my $c = $iter->next) +{ + push(@answer, "[" . join(",", @{$c}) . "]"); +} + +say "[ ", join(", ", @answer), " ]"; diff --git a/challenge-067/arne-sommer/raku/ch-1.raku b/challenge-067/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..3fab02ba3d --- /dev/null +++ b/challenge-067/arne-sommer/raku/ch-1.raku @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int $m where $m > 0, Int $n where $n > 0); + +say "[ ", (1..$m).combinations($n).map({ "[{ $_.join(",") }]" }).join(", "), " ]"; diff --git a/challenge-067/arne-sommer/raku/ch-2.raku b/challenge-067/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..943276c439 --- /dev/null +++ b/challenge-067/arne-sommer/raku/ch-2.raku @@ -0,0 +1,41 @@ +#! /usr/bin/env raku + +unit sub MAIN ($S where $S.chars > 0); + +my %button; + +%button<1> = <_ , @>; +%button<2> = <a b c>; +%button<3> = <d e f>; +%button<4> = <g h i>; +%button<5> = <j k l>; +%button<6> = <m n o>; +%button<7> = <p q r s>; +%button<8> = <t u v>; +%button<9> = <w x y z>; +%button<*> = (' ',); + +my @solutions; + +off-we-go("", $S); + +say "[", @solutions.map({ "\"{ $_ }\"" }).join(", "), "]" if @solutions; + +sub off-we-go ($so-far, $to-do) +{ + if $to-do.chars == 0 + { + @solutions.push: $so-far; + return; + } + + my $current = $to-do.substr(0,1); + my $remainder = $to-do.substr(1); + + die "Illegal character $current" unless %button{$current}; + + for @(%button{$current}) -> $character + { + off-we-go($so-far ~ $character, $remainder); + } +} diff --git a/challenge-067/arne-sommer/raku/letterphone b/challenge-067/arne-sommer/raku/letterphone new file mode 100755 index 0000000000..943276c439 --- /dev/null +++ b/challenge-067/arne-sommer/raku/letterphone @@ -0,0 +1,41 @@ +#! /usr/bin/env raku + +unit sub MAIN ($S where $S.chars > 0); + +my %button; + +%button<1> = <_ , @>; +%button<2> = <a b c>; +%button<3> = <d e f>; +%button<4> = <g h i>; +%button<5> = <j k l>; +%button<6> = <m n o>; +%button<7> = <p q r s>; +%button<8> = <t u v>; +%button<9> = <w x y z>; +%button<*> = (' ',); + +my @solutions; + +off-we-go("", $S); + +say "[", @solutions.map({ "\"{ $_ }\"" }).join(", "), "]" if @solutions; + +sub off-we-go ($so-far, $to-do) +{ + if $to-do.chars == 0 + { + @solutions.push: $so-far; + return; + } + + my $current = $to-do.substr(0,1); + my $remainder = $to-do.substr(1); + + die "Illegal character $current" unless %button{$current}; + + for @(%button{$current}) -> $character + { + off-we-go($so-far ~ $character, $remainder); + } +} diff --git a/challenge-067/arne-sommer/raku/numcom b/challenge-067/arne-sommer/raku/numcom new file mode 100755 index 0000000000..3fab02ba3d --- /dev/null +++ b/challenge-067/arne-sommer/raku/numcom @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int $m where $m > 0, Int $n where $n > 0); + +say "[ ", (1..$m).combinations($n).map({ "[{ $_.join(",") }]" }).join(", "), " ]"; |
