aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-07-21 17:26:21 +0100
committerGitHub <noreply@github.com>2020-07-21 17:26:21 +0100
commit6d9ade3607d378530717732a7040348dd118a128 (patch)
tree5df7ea0fea7c447b5aa5212e3e11be4ce7862938
parentd96c3b3776fbb99ff873a07312075e0d34ea77c1 (diff)
parent5ab4a89123407a37fc8c78c8e6697d6c088b418a (diff)
downloadperlweeklychallenge-club-6d9ade3607d378530717732a7040348dd118a128.tar.gz
perlweeklychallenge-club-6d9ade3607d378530717732a7040348dd118a128.tar.bz2
perlweeklychallenge-club-6d9ade3607d378530717732a7040348dd118a128.zip
Merge pull request #1964 from waltman/branch-for-challenge-070
Branch for challenge 070
-rw-r--r--challenge-070/walt-mankowski/perl/ch-1.pl51
-rw-r--r--challenge-070/walt-mankowski/perl/ch-2.pl60
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-070/walt-mankowski/perl/ch-1.pl b/challenge-070/walt-mankowski/perl/ch-1.pl
new file mode 100644
index 0000000000..823119a0da
--- /dev/null
+++ b/challenge-070/walt-mankowski/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+
+# TASK #1 › Character Swapping
+# Submitted by: Mohammad S Anwar
+#
+# 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
+
+my ($s, $c, $o) = @ARGV;
+my $n = length($s);
+
+for my $i (1..$c) {
+ (substr($s, $i % $n, 1), substr($s, ($i + $o) % $n, 1)) =
+ (substr($s, ($i + $o) % $n, 1), substr($s, $i % $n, 1));
+}
+
+say $s;
diff --git a/challenge-070/walt-mankowski/perl/ch-2.pl b/challenge-070/walt-mankowski/perl/ch-2.pl
new file mode 100644
index 0000000000..c908d9b6b1
--- /dev/null
+++ b/challenge-070/walt-mankowski/perl/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+
+# TASK #2 › Gray Code Sequence
+# Submitted by: Mohammad S Anwar
+#
+# 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]
+
+my $n = $ARGV[0];
+my @S = (0, 1, 3, 2);
+
+for my $i (3..$n) {
+ @S = gray_code($i, @S);
+}
+
+say "@S";
+
+sub gray_code($n, @S) {
+ my @S2 = map { "1$_" } reverse map { scalar sprintf "%.*b", $n-1, $_ } @S;
+ return @S, map { eval "0b$_" } @S2;
+}