aboutsummaryrefslogtreecommitdiff
path: root/challenge-070
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-07-26 14:05:46 +0200
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-07-26 14:05:46 +0200
commit632b5536a3b9640253b93009dc58bfcd4c87b419 (patch)
tree9f2651b79dacbe0aa08104f8d3c22dceb1df5125 /challenge-070
parent43d67dc2e00080bd3020c7cbf9d2ccaec8c21cf1 (diff)
downloadperlweeklychallenge-club-632b5536a3b9640253b93009dc58bfcd4c87b419.tar.gz
perlweeklychallenge-club-632b5536a3b9640253b93009dc58bfcd4c87b419.tar.bz2
perlweeklychallenge-club-632b5536a3b9640253b93009dc58bfcd4c87b419.zip
Solution to challenge 070 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-070')
-rw-r--r--challenge-070/noud/raku/ch-1.p644
-rw-r--r--challenge-070/noud/raku/ch-2.p651
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-070/noud/raku/ch-1.p6 b/challenge-070/noud/raku/ch-1.p6
new file mode 100644
index 0000000000..bd7c8572af
--- /dev/null
+++ b/challenge-070/noud/raku/ch-1.p6
@@ -0,0 +1,44 @@
+# You are given a string $S of size $N.
+#
+# You are also given swap count $C and offset $O such that $C >= 1, $O >= 1, $C
+# <= $O and $C + $O <= $N.
+#
+# UPDATE: 2020-07-20 16:10:00
+# Pete Houston suggested to put additional constraint i.e. $C <= $O. He
+# presented the use case $S = 'abcd', $C = 2, $O = 1.
+#
+# Write a script to perform character swapping like below:
+#
+# $S[ 1 % $N ] <=> $S[ (1 + $O) % $N ]
+# $S[ 2 % $N ] <=> $S[ (2 + $O) % $N ]
+# $S[ 3 % $N ] <=> $S[ (3 + $O) % $N ]
+# ...
+# ...
+# $S[ $C % $N ] <=> $S[ ($C + $O) % $N ]
+#
+# Example 1
+#
+# Input:
+# $S = 'perlandraku'
+# $C = 3
+# $O = 4
+#
+# Character Swapping:
+# swap 1: e <=> n = pnrlaedraku
+# swap 2: r <=> d = pndlaerraku
+# swap 3: l <=> r = pndraerlaku
+#
+# Output:
+# pndraerlaku
+
+
+sub char-swap($S, $C, $O) {
+ my $new-S = $S;
+ for 1..$C -> $i {
+ $new-S.substr-rw($i, 1) = $S.comb[$i + $O];
+ $new-S.substr-rw($i + $O, 1) = $S.comb[$i];
+ }
+ return $new-S;
+}
+
+char-swap('perlandraku', 3, 4).say;
diff --git a/challenge-070/noud/raku/ch-2.p6 b/challenge-070/noud/raku/ch-2.p6
new file mode 100644
index 0000000000..cc952b21d8
--- /dev/null
+++ b/challenge-070/noud/raku/ch-2.p6
@@ -0,0 +1,51 @@
+# You are given an integer 2 <= $N <= 5.
+#
+# Write a script to generate $N-bit gray code sequence.
+# 2-bit Gray Code Sequence
+#
+# [0, 1, 3, 2]
+#
+# To generate the 3-bit Gray code sequence from the 2-bit Gray code sequence,
+# follow the step below:
+#
+# 2-bit Gray Code sequence
+# [0, 1, 3, 2]
+#
+# Binary form of the sequence
+# a) S1 = [00, 01, 11, 10]
+#
+# Reverse of S1
+# b) S2 = [10, 11, 01, 00]
+#
+# Prefix all entries of S1 with '0'
+# c) S1 = [000, 001, 011, 010]
+#
+# Prefix all entries of S2 with '1'
+# d) S2 = [110, 111, 101, 100]
+#
+# Concatenate S1 and S2 gives 3-bit Gray Code sequence
+# e) [000, 001, 011, 010, 110, 111, 101, 100]
+#
+# 3-bit Gray Code sequence
+# [0, 1, 3, 2, 6, 7, 5, 4]
+#
+# Example
+#
+# Input: $N = 4
+#
+# Output: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8]
+
+sub gray-code-sequence($n) {
+ if ($n == 1) {
+ return [0, 1];
+ }
+ my $seq1 = gray-code-sequence($n - 1);
+ my $seq2 = [2**($n - 1) + $_ for $seq1.reverse];
+ return [|($seq1), |($seq2)];
+}
+
+gray-code-sequence(1).say;
+gray-code-sequence(2).say;
+gray-code-sequence(3).say;
+gray-code-sequence(4).say;
+gray-code-sequence(5).say;