aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-067/wanderdoc/R/ch-1.R7
-rw-r--r--challenge-067/wanderdoc/R/ch-2.R17
-rw-r--r--challenge-067/wanderdoc/perl/ch-1.pl52
-rw-r--r--challenge-067/wanderdoc/perl/ch-2.pl97
4 files changed, 173 insertions, 0 deletions
diff --git a/challenge-067/wanderdoc/R/ch-1.R b/challenge-067/wanderdoc/R/ch-1.R
new file mode 100644
index 0000000000..6fbc030cdc
--- /dev/null
+++ b/challenge-067/wanderdoc/R/ch-1.R
@@ -0,0 +1,7 @@
+x <- 5
+y <- 1:x
+z <- 2
+
+combn(y,z)
+# or:
+as.data.frame(t(unlist(apply(combn(y,z), 2, list), recursive = FALSE)), row.names = c("")) \ No newline at end of file
diff --git a/challenge-067/wanderdoc/R/ch-2.R b/challenge-067/wanderdoc/R/ch-2.R
new file mode 100644
index 0000000000..9d89c4adc1
--- /dev/null
+++ b/challenge-067/wanderdoc/R/ch-2.R
@@ -0,0 +1,17 @@
+tel <- list(
+ "1" = c("-", ",", "@"),
+ "2" = c("A", "B", "C"),
+
+ "3" = c("D", "E", "F"),
+ "4" = c("G", "H", "I"),
+ "5" = c("J", "K", "L"),
+ "6" = c("M", "N", "O"),
+ "7" = c("P", "Q", "R", "S"),
+ "8" = c("T", "U", "V"),
+ "9" = c("W", "X", "Y", "Z")
+)
+
+string <- c("35")
+selection <- unlist(strsplit(string, NULL))
+
+expand.grid(lapply(tel[selection], tolower)) \ No newline at end of file
diff --git a/challenge-067/wanderdoc/perl/ch-1.pl b/challenge-067/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..180abec3e5
--- /dev/null
+++ b/challenge-067/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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] ]
+=cut
+
+
+
+
+use Algorithm::Combinatorics qw(combinations);
+
+
+my $m = shift // 5;
+my $n = shift // 2;
+die "Two positive integers, m and n, where m > n!$/" unless
+($m =~ /^[0-9]+$/ and $n =~ /^[0-9]+$/ and $n > 0 and $m > $n);
+
+
+
+my @memoize; # For the factorial function (s. below).
+# This, in turn, only to mimic the output with commas.
+$memoize[1] = 1;
+my $num_com = factorial($m) /( factorial($n) * factorial($m - $n ) );
+my $iter = combinations([1 .. $m], $n);
+my $counter;
+while (my $c = $iter->next)
+{
+ print "[", join(",", @$c), "]";
+ $counter++;
+ print ", " if $counter < $num_com;
+}
+
+
+sub factorial # From "Mastering Perl".
+{
+ my $num = $_[0];
+
+ return $memoize[$num] if $memoize[$num];
+ for ( @memoize .. $num )
+ {
+ $memoize[$_] = $memoize[$_ - 1] * $_;
+
+ }
+ return $memoize[$num];
+} \ No newline at end of file
diff --git a/challenge-067/wanderdoc/perl/ch-2.pl b/challenge-067/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..c7c9ec3092
--- /dev/null
+++ b/challenge-067/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,97 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a digit string $S. Write a script to print all possible letter combinations that the given digit string could represent.
+Input: $S = '35'
+Output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"].
+=cut
+
+
+
+
+
+# Solution with glob. It would not work with button "1" because of comma.
+# However there are no _letters_ on that button :-)
+
+use List::Util qw(reduce);
+
+my %phone =
+(
+ "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 @memoize; # For the factorial function (s. below).
+# This, in turn, only to mimic the output with commas.
+$memoize[1] = 1;
+my $string = shift || 35;
+die "At least two digits as a string!$/" unless (length($string) > 1 and $string =~ /^[0-9]+$/);
+
+
+my @buttons = split(//,$string);
+
+my $letters = join('',map "{" . join(",", @$_) . "}", @phone{@buttons});
+
+my $num_prod = reduce {$a * $b}
+ map factorial(scalar(@$_))/factorial(scalar(@$_) - 1), @phone{@buttons};
+
+my $counter;
+
+while ( my $pair = glob $letters )
+{
+
+ print lc "\"$pair\"";
+ $counter++;
+ print ", " if $counter < $num_prod;
+}
+print $/;
+
+# To include the signs on the button "1" I would use a CPAN module.
+
+# Either:
+use Math::Cartesian::Product;
+
+$counter = 0;
+cartesian {$counter++; print lc '"' . join('', @_) . '"' . ($counter < $num_prod ? ', ' : '')} @phone{@buttons};
+print $/;
+
+# or:
+use Set::CrossProduct;
+
+$counter = 0;
+
+my $iterator = Set::CrossProduct->new( [@phone{@buttons}] );
+
+my $number_of_tuples = $iterator->cardinality; # or reuse $num_prod;
+
+while ( my $tuple = $iterator->get )
+{
+ print lc '"' . join('',@$tuple) . '"';
+ $counter++;
+ print ", " if $counter < $number_of_tuples;
+}
+print $/;
+
+
+sub factorial # From "Mastering Perl".
+{
+ my $num = $_[0];
+ return $memoize[$num] if $memoize[$num];
+
+ for ( @memoize .. $num )
+ {
+ $memoize[$_] = $memoize[$_ - 1] * $_;
+
+ }
+ return $memoize[$num];
+} \ No newline at end of file