diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-07-06 03:17:26 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-07-06 03:17:26 +0100 |
| commit | 614d2465fc4eac0cf954bee787ec72527138194d (patch) | |
| tree | ee5213d2c947a9947917a4cbb9bb639b6108fd9d /challenge-067 | |
| parent | 547c2df06e540d00a81f2667b60a2ef10e18c872 (diff) | |
| download | perlweeklychallenge-club-614d2465fc4eac0cf954bee787ec72527138194d.tar.gz perlweeklychallenge-club-614d2465fc4eac0cf954bee787ec72527138194d.tar.bz2 perlweeklychallenge-club-614d2465fc4eac0cf954bee787ec72527138194d.zip | |
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-067')
| -rw-r--r-- | challenge-067/colin-crain/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-067/colin-crain/perl/ch-1.pl | 62 | ||||
| -rw-r--r-- | challenge-067/colin-crain/perl/ch-2.pl | 72 | ||||
| -rw-r--r-- | challenge-067/colin-crain/raku/ch-1.p6 | 29 | ||||
| -rw-r--r-- | challenge-067/colin-crain/raku/ch-2.p6 | 65 |
5 files changed, 229 insertions, 0 deletions
diff --git a/challenge-067/colin-crain/blog.txt b/challenge-067/colin-crain/blog.txt new file mode 100644 index 0000000000..089611788d --- /dev/null +++ b/challenge-067/colin-crain/blog.txt @@ -0,0 +1 @@ +https://colincrain.wordpress.com/2020/07/06/combos-convos-and-cellphones/ diff --git a/challenge-067/colin-crain/perl/ch-1.pl b/challenge-067/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..81a8714697 --- /dev/null +++ b/challenge-067/colin-crain/perl/ch-1.pl @@ -0,0 +1,62 @@ +#! /opt/local/bin/perl +# +# number_combo_convo.pl +# +# TASK #1 › Number Combinations +# Submitted by: Mohammad S Anwar +# +# You are given two integers $max and $elems. Write a script print all +# possible combinations of $elems numbers from the list 1 2 3 … $max. +# +# Every combination should be sorted i.e. [2,3] is valid +# combination but [3,2] is not. +# +# Example: +# Input: $max = 5, $elems = 2 +# +# Output: [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ] +# +# +# 2020 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +use warnings; +use strict; +use feature ":5.26"; + +## ## ## ## ## MAIN: + +my ($max, $elems) = @ARGV; + +($elems > $max or @ARGV < 2) and do { + say "Usage: ./number_combos.pl max elements + max cannot be less than # of elements"; + exit }; + +my @list = add_elements( $max, $elems, [[]] )->@*; + +say ( "[" . (join ', ', map { "[". (join ',', $_->@*) . "]" } @list) . "]"); + + +## ## ## ## ## SUBS: + +sub add_elements { + my ($max, $elems, $list) = @_; + return $list if $list->[0]->@* == $elems; + + my @newlist = (); + + my $pos = $list->[0]->@* + 1; ## this position, elems of prev list + 1 + + for my $combo ( $list->@* ) { + my $prev = $combo->[-1] // 0; + my $start = $prev + 1; ## value of last elem in list + 1 + my $end = $max - $elems + $pos; ## max - length + position + for ($start .. $end ) { + push @newlist, [ $combo->@*, $_ ] + } + } + return add_elements( $max, $elems, \@newlist); +} diff --git a/challenge-067/colin-crain/perl/ch-2.pl b/challenge-067/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..fc902fcfd0 --- /dev/null +++ b/challenge-067/colin-crain/perl/ch-2.pl @@ -0,0 +1,72 @@ +#! /opt/local/bin/perl +# +# hooked_on_phonics.pl +# +# 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 +# 2 --> ABC +# 3 --> DEF +# 4 --> GHI +# 5 --> JKL +# 6 --> MNO +# 7 --> PQRS +# 8 --> TUV +# 9 --> WXYZ +# +# +# Example: +# Input: $S = '35' +# +# Output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"]. +# +# 2020 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + +use warnings; +use strict; +use feature ":5.26"; + +## ## ## ## ## MAIN: + +my $input = $ARGV[0] // '5273'; +$input =~ s/\W//g; ## tolerate phone number formats, strip punct chars +($input =~ /[^2-9]/) and + die "only numbers between 2 through 9 can be alphabetized\n"; + +my @digits = split //, $input; + +my %encode = ( 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 $list = $encode{(shift @digits)}; + +my @list = make_strings(\%encode, \@digits, $list)->@*; +say $_ for @list; + +## ## ## ## ## SUBS: + +sub make_strings { + my ($encode, $digits, $list) = @_; + return $list unless $digits->@*; + + my @newlist = (); + my $digit = shift $digits->@*; + for my $str ( $list->@* ) { + for ( $encode->{$digit}->@* ) { + push @newlist, $str . $_; + } + } + return make_strings( $encode, $digits, \@newlist); +} diff --git a/challenge-067/colin-crain/raku/ch-1.p6 b/challenge-067/colin-crain/raku/ch-1.p6 new file mode 100644 index 0000000000..b73604d82c --- /dev/null +++ b/challenge-067/colin-crain/raku/ch-1.p6 @@ -0,0 +1,29 @@ +#!/usr/bin/env perl6 +# +# +# number_combo_convo.raku +# +# TASK #1 › Number Combinations +# Submitted by: Mohammad S Anwar +# +# You are given two integers $max and $elems. Write a script print all +# possible combinations of $elems numbers from the list 1 2 3 … $max. +# +# Every combination should be sorted i.e. [2,3] is valid +# combination but [3,2] is not. +# +# Example: +# Input: $max = 5, $elems = 2 +# +# Output: [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ] +# +# +# 2020 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +sub MAIN ( Int $max = 5, Int $elems = 3 ) { + + .say for (1..$max).combinations: $elems; +} diff --git a/challenge-067/colin-crain/raku/ch-2.p6 b/challenge-067/colin-crain/raku/ch-2.p6 new file mode 100644 index 0000000000..d102b4e96f --- /dev/null +++ b/challenge-067/colin-crain/raku/ch-2.p6 @@ -0,0 +1,65 @@ +#!/usr/bin/env perl6 +# +# +# hooked_on_phonics.raku +# +# 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 +# 2 --> ABC +# 3 --> DEF +# 4 --> GHI +# 5 --> JKL +# 6 --> MNO +# 7 --> PQRS +# 8 --> TUV +# 9 --> WXYZ +# +# +# Example: +# Input: $S = '35' +# +# Output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"]. +# +# 2020 colin crain +# +# # # # # # # # # # # # # # # # # # # + + +sub MAIN ( Int $input = 527 ) { + + my @digits = $input.comb; + + my %encode = ( 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 @list = |%encode{@digits.shift}; + + @list = make_strings(%encode, @digits, @list); + .say for @list; + +} + +sub make_strings (%encode, @digits, @list) { + return @list unless @digits.elems > 0; + + my @newlist; + my $digit = @digits.shift; + for @list -> $str { + for ( %encode{$digit}.list ) { + @newlist.push: $str ~ $_; + } + } + return make_strings( %encode, @digits, @newlist); +} |
