aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkevincolyer <kevin@thecolyers.net>2020-03-28 21:14:29 +0000
committerkevincolyer <>2020-03-28 21:14:29 +0000
commite4de6aefae3d4ab8759ffaf4f4ee15ac971f58c4 (patch)
tree630d8d740dd85ed84f5f1aad84f25504545fb57b
parentf1f5e7ce40a1c3425f94616eafd7b5e7304b9cbe (diff)
downloadperlweeklychallenge-club-e4de6aefae3d4ab8759ffaf4f4ee15ac971f58c4.tar.gz
perlweeklychallenge-club-e4de6aefae3d4ab8759ffaf4f4ee15ac971f58c4.tar.bz2
perlweeklychallenge-club-e4de6aefae3d4ab8759ffaf4f4ee15ac971f58c4.zip
Challenge-053 solutions by kevincolyer
-rw-r--r--challenge-053/kevin-colyer/README2
-rw-r--r--challenge-053/kevin-colyer/raku/ch-1-bug.p670
-rw-r--r--challenge-053/kevin-colyer/raku/ch-1.p637
-rw-r--r--challenge-053/kevin-colyer/raku/ch-2.p645
l---------challenge-053/kevincolyer1
5 files changed, 155 insertions, 0 deletions
diff --git a/challenge-053/kevin-colyer/README b/challenge-053/kevin-colyer/README
index 32aa940083..0e95de6a44 100644
--- a/challenge-053/kevin-colyer/README
+++ b/challenge-053/kevin-colyer/README
@@ -1 +1,3 @@
Solutions by Kevin Colyer.
+
+I think there is some odd bug - my initial tries at rotating the matrix affected the original matrix even when I DIDN'T specify is rw in the sub.
diff --git a/challenge-053/kevin-colyer/raku/ch-1-bug.p6 b/challenge-053/kevin-colyer/raku/ch-1-bug.p6
new file mode 100644
index 0000000000..9ff19c05a6
--- /dev/null
+++ b/challenge-053/kevin-colyer/raku/ch-1-bug.p6
@@ -0,0 +1,70 @@
+#!perl6
+# Task 1 Challenge 053 Solution by kevincolyer
+# Rotate Matrix
+# Write a script to rotate the followin matrix by given 90/180/270
+# degrees clockwise.
+# [ 1, 2, 3 ][ 4, 5, 6 ][ 7, 8, 9 ]For example, if you rotate by
+# 90 degrees then expected result should be like below
+# [ 7, 4, 1 ][ 8, 5, 2 ][ 9, 6, 3 ]
+
+sub ArotateMatrix(@matrix,$deg) {
+ say 'A: my @newMat=@matrix';
+ my @newMat=@matrix;
+ for ^@matrix.elems -> $row {
+ for ^@matrix[0].elems -> $col {
+ @newMat[$col][$row] = @matrix[$row][$col];
+ }
+ }
+ return @newMat;
+}
+
+sub BrotateMatrix(@matrix,$deg) {
+ say 'B: my @newMat=@matrix.clone';
+ my @newMat=@matrix.clone;
+ for ^@matrix.elems -> $row {
+ for ^@matrix[0].elems -> $col {
+ @newMat[$col][$row] = @matrix[$row][$col];
+ }
+ }
+ return @newMat;
+}
+
+sub CrotateMatrix(@matrix is copy,$deg) {
+ say 'C: my @newMat=@matrix.clone (called with @matrix is copy)';
+ my @newMat=@matrix.clone;
+ say @newMat.perl;
+ for ^@matrix.elems -> $row {
+ for ^@matrix[0].elems -> $col {
+ @newMat[$col][$row] = @matrix[$row][$col];
+ }
+ }
+ return @newMat;
+}
+sub DrotateMatrix(@matrix,$deg) {
+ say 'D: my @newMat; (rebuilt)';
+ my @newMat;
+ @newMat.append: [] xx @matrix[0].elems;
+ for ^@matrix.elems -> $row {
+ for ^@matrix[0].elems -> $col {
+ @newMat[$col][$row] = @matrix[$row][$col];
+ }
+ }
+ return @newMat;
+}
+
+my @matrix=[1,2,3], [4,5,6], [7,8,9];
+say "\nin: " ~ @matrix.perl;
+say "out: " ~ ArotateMatrix(@matrix,90).perl;
+say "org: " ~ @matrix.perl;
+@matrix=[1,2,3], [4,5,6], [7,8,9];
+say "\nin: " ~ @matrix.perl;
+say "out: " ~ BrotateMatrix(@matrix,90).perl;
+say "org: " ~ @matrix.perl;
+@matrix=[1,2,3], [4,5,6], [7,8,9];
+say "\nin: " ~ @matrix.perl;
+say "out: " ~ CrotateMatrix(@matrix,90).perl;
+say "org: " ~ @matrix.perl;
+@matrix=[1,2,3], [4,5,6], [7,8,9];
+say "\nin: " ~ @matrix.perl;
+say "out: " ~ DrotateMatrix(@matrix,90).perl;
+say "org: " ~ @matrix.perl;
diff --git a/challenge-053/kevin-colyer/raku/ch-1.p6 b/challenge-053/kevin-colyer/raku/ch-1.p6
new file mode 100644
index 0000000000..f67c3b668a
--- /dev/null
+++ b/challenge-053/kevin-colyer/raku/ch-1.p6
@@ -0,0 +1,37 @@
+#!perl6
+# Task 1 Challenge 053 Solution by kevincolyer
+# Rotate Matrix
+# Write a script to rotate the followin matrix by given 90/180/270
+# degrees clockwise.
+# [ 1, 2, 3 ][ 4, 5, 6 ][ 7, 8, 9 ]For example, if you rotate by
+# 90 degrees then expected result should be like below
+# [ 7, 4, 1 ][ 8, 5, 2 ][ 9, 6, 3 ]
+
+sub rotateMatrix90(@matrix) {
+ my @newMat;
+ my $high=@matrix.elems;
+ my $wide=@matrix[0].elems;
+ @newMat.append: [] xx @matrix[0].elems;
+ for ^@matrix.elems -> $row {
+ for ^$wide -> $col {
+ @newMat[$col][$row] = @matrix[$high-1-$row][$col];
+ }
+ }
+ return @newMat;
+}
+
+sub rotateMatrix(@matrix is copy,$degrees is copy) {
+ die "Can't rotate by $degrees" unless $degrees == any(90,180,270);
+ while $degrees {
+ $degrees-=90;
+ @matrix=rotateMatrix90(@matrix);
+ }
+ return @matrix;
+}
+
+my @matrix=[1,2,3], [4,5,6], [7,8,9];
+say "\nin: " ~ @matrix.perl;
+say "out: 90 " ~ rotateMatrix(@matrix,90).perl;
+say "out: 180" ~ rotateMatrix(@matrix,180).perl;
+say "out: 270" ~ rotateMatrix(@matrix,270).perl;
+say "out: 271" ~ rotateMatrix(@matrix,271).perl;
diff --git a/challenge-053/kevin-colyer/raku/ch-2.p6 b/challenge-053/kevin-colyer/raku/ch-2.p6
new file mode 100644
index 0000000000..255b3c0024
--- /dev/null
+++ b/challenge-053/kevin-colyer/raku/ch-2.p6
@@ -0,0 +1,45 @@
+#!perl6
+# Task 2 Challenge 053 Solution by kevincolyer
+# Vowel Strings
+# Write a script to accept an integer 1 <= N <= 5 that would
+# print all possible strings of size N formed by using only vowels
+# (a, e, i, o, u).
+# The string should follow the following rules:
+# 'a’ can only be followed by ‘e’ and ‘i’.
+# ‘e’ can only be followed by ‘i’.
+# ‘i’ can only be followed by ‘a’, ‘e’, ‘o’, and ‘u’.
+# ‘o’ can only be followed by ‘a’ and ‘u’.
+# ‘u’ can only be followed by ‘o’ and ‘e’.
+# For example, if the given integer N = 2 then script should print
+# the following strings:
+# aeaieiiaioiuieoaouuoue
+
+my %chain = 'a' => <e i>,
+ 'e' => <i>,
+ 'i' => <a e o u>,
+ 'o' => <a u>,
+ 'u' => <o e>;
+
+sub possibleStrings(Int $size) {
+ die "1 <= size <= 5" unless 1 <= $size <= 5;
+ my @solutions;
+ for %chain.keys -> $start {
+ @solutions.append: vowelChains($start,$size);
+ }
+ return @solutions.sort;
+}
+
+sub vowelChains($start,$depth) {
+ return '' if $depth == 0;
+ my @solutions;
+ for %chain{$start}.values -> $next {
+ # say "$start -> $next";
+ @solutions.append: $start ~ $_ for vowelChains($next,$depth-1);
+ }
+ return @solutions.unique.sort;
+
+}
+
+sub MAIN($size) {
+ possibleStrings($size)>>.say;
+}
diff --git a/challenge-053/kevincolyer b/challenge-053/kevincolyer
new file mode 120000
index 0000000000..8fc47c15c2
--- /dev/null
+++ b/challenge-053/kevincolyer
@@ -0,0 +1 @@
+kevin-colyer \ No newline at end of file