aboutsummaryrefslogtreecommitdiff
path: root/challenge-045
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-02-01 20:35:06 +0100
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-02-01 20:35:06 +0100
commit899af4a1a73758d31dce0292e724c04da0abf1b0 (patch)
tree6b1169a1b30795c178b34ca1bb95e81a821ceca2 /challenge-045
parent4c3c209f6c4285d1ece0915d31cd9f31ee2e43b0 (diff)
downloadperlweeklychallenge-club-899af4a1a73758d31dce0292e724c04da0abf1b0.tar.gz
perlweeklychallenge-club-899af4a1a73758d31dce0292e724c04da0abf1b0.tar.bz2
perlweeklychallenge-club-899af4a1a73758d31dce0292e724c04da0abf1b0.zip
Solutions to challenge 45 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-045')
-rw-r--r--challenge-045/noud/raku/ch-1.p635
-rw-r--r--challenge-045/noud/raku/ch-2.p68
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-045/noud/raku/ch-1.p6 b/challenge-045/noud/raku/ch-1.p6
new file mode 100644
index 0000000000..4881faee96
--- /dev/null
+++ b/challenge-045/noud/raku/ch-1.p6
@@ -0,0 +1,35 @@
+# Square Secret Code
+#
+# The square secret code mechanism first removes any space from the original
+# message. Then it lays down the message in a row of 8 columns. The coded
+# message is then obtained by reading down the columns going left to right.
+#
+# For example, the message is “The quick brown fox jumps over the lazy dog”.
+#
+# Then the message would be laid out as below:
+#
+# thequick
+# brownfox
+# jumpsove
+# rthelazy
+# dog
+#
+# The code message would be as below:
+#
+# tbjrd hruto eomhg qwpe unsl ifoa covz kxey
+#
+# Write a script that accepts a message from command line and prints the
+# equivalent coded message.
+
+sub encode($str) {
+ # Lowercase all letters and remove white spaces.
+ my $stripped = $str.lc.subst(/\s/, '', :g);
+
+ # Extend string to multiple of 8 and rotate around 8.
+ my @r = ($stripped ~ " " x (8 - $stripped.chars % 8)).comb.rotor(8, :partial);
+
+ # Take Cartesian product of the rotated string.
+ return ([Z~] @r).map({ $_.trim }).join(' ');
+}
+
+say encode("The quick brown fox jumps over the lazy dog");
diff --git a/challenge-045/noud/raku/ch-2.p6 b/challenge-045/noud/raku/ch-2.p6
new file mode 100644
index 0000000000..3cfa047774
--- /dev/null
+++ b/challenge-045/noud/raku/ch-2.p6
@@ -0,0 +1,8 @@
+# Source Dumper
+#
+# Write a script that dumps its own source code. For example, say, the script
+# name is ch-2.pl then the following command should returns nothing.
+#
+# $ perl ch-2.pl | diff - ch-2.pl
+
+$?FILE.IO.slurp.trim.say;