aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-03-28 20:16:01 +0100
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-03-28 20:16:01 +0100
commit173449df2be4893282345077f09b1c1a95c6812c (patch)
treed0f87da3bb71fd92a88fdead921ee4f93b1b6ffe
parent48f1c6f8ca8133b8e461c03004ca9fd3977ac6b0 (diff)
downloadperlweeklychallenge-club-173449df2be4893282345077f09b1c1a95c6812c.tar.gz
perlweeklychallenge-club-173449df2be4893282345077f09b1c1a95c6812c.tar.bz2
perlweeklychallenge-club-173449df2be4893282345077f09b1c1a95c6812c.zip
Solution to challenge 53 task 1 and 2 in Raku by Noud
-rw-r--r--challenge-053/noud/raku/ch-1.p673
-rw-r--r--challenge-053/noud/raku/ch-2.p651
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-053/noud/raku/ch-1.p6 b/challenge-053/noud/raku/ch-1.p6
new file mode 100644
index 0000000000..215d67fa9a
--- /dev/null
+++ b/challenge-053/noud/raku/ch-1.p6
@@ -0,0 +1,73 @@
+# 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 print-matrix(@N) {
+ say $_ for @N;
+}
+
+# Flip matrix in diagonal
+sub diag-flip(@N) {
+ for 0..^@N.elems -> $i {
+ for 0..^$i -> $j {
+ my $t = @N[$i][$j];
+ @N[$i][$j] = @N[$j][$i];
+ @N[$j][$i] = $t;
+ }
+ }
+ @N;
+}
+
+# Flip matrix horizontal
+sub horz-flip(@N) {
+ for 0..^@N.elems -> $i {
+ for 0..^(@N.elems / 2) -> $j {
+ my $t = @N[$i][$j];
+ @N[$i][$j] = @N[$i][@N.elems - $j - 1];
+ @N[$i][@N.elems - $j - 1] = $t;
+ }
+ }
+ @N;
+}
+
+# The horizontal and diagonal flip together generate the full symmetry group of
+# the square, also called the Octic group D4. Rotating 90 degrees is a symmetry
+# of the square (or matrix). Therefore we can find a combination of horizontal
+# and diagonal flipping that's equal to rotating 90 degrees.
+sub rotate90(@N) {
+ horz-flip(diag-flip(@N));
+}
+
+sub rotate180(@N) {
+ rotate90(rotate90(@N));
+}
+
+sub rotate270(@N) {
+ rotate90(rotate180(@N));
+}
+
+
+my @M = [[1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9]];
+
+say 'Original matrix:';
+print-matrix(@M);
+say 'Rotate 90 degrees:';
+print-matrix(rotate90(@M));
+say 'Rotate 180 degrees:';
+print-matrix(rotate90(@M));
+say 'Rotate 270 degrees:';
+print-matrix(rotate90(@M));
diff --git a/challenge-053/noud/raku/ch-2.p6 b/challenge-053/noud/raku/ch-2.p6
new file mode 100644
index 0000000000..54762ba155
--- /dev/null
+++ b/challenge-053/noud/raku/ch-2.p6
@@ -0,0 +1,51 @@
+# 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:
+#
+# ae
+# ai
+# ei
+# ia
+# io
+# iu
+# ie
+# oa
+# ou
+# uo
+# ue
+
+my %rules = 'a' => ['e', 'i'],
+ 'e' => ['i'],
+ 'i' => ['a', 'e', 'o', 'u'],
+ 'o' => ['a', 'u'],
+ 'u' => ['o', 'e'];
+
+sub vowel-strings($n) {
+ if ($n == 1) {
+ return [['a'], ['e'], ['i'], ['o'], ['u']];
+ }
+
+ my @ret = [];
+ for vowel-strings($n - 1) -> @a {
+ for %rules{@a[*-1]}[*;*] -> $e {
+ @ret.push([|(@a), $e]);
+ }
+ }
+ return @ret;
+}
+
+sub MAIN($n) {
+ $_.say for vowel-strings($n).map({.join});
+}