aboutsummaryrefslogtreecommitdiff
path: root/challenge-070
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-19 15:05:58 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-19 15:05:58 +0100
commit4ed1edb56f6452d5b64f2ec143345317955f31f8 (patch)
treeaff09c60770052d432d3fd4c38c8f836bbb39a11 /challenge-070
parent62ff27016de38a9f62cecb5de65dd4bc1db20ecc (diff)
downloadperlweeklychallenge-club-4ed1edb56f6452d5b64f2ec143345317955f31f8.tar.gz
perlweeklychallenge-club-4ed1edb56f6452d5b64f2ec143345317955f31f8.tar.bz2
perlweeklychallenge-club-4ed1edb56f6452d5b64f2ec143345317955f31f8.zip
Add Perl solution to challenge 070
Diffstat (limited to 'challenge-070')
-rw-r--r--challenge-070/paulo-custodio/Makefile2
-rw-r--r--challenge-070/paulo-custodio/README1
-rw-r--r--challenge-070/paulo-custodio/perl/ch-1.pl59
-rw-r--r--challenge-070/paulo-custodio/perl/ch-2.pl71
-rw-r--r--challenge-070/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-070/paulo-custodio/t/test-2.yaml5
6 files changed, 143 insertions, 0 deletions
diff --git a/challenge-070/paulo-custodio/Makefile b/challenge-070/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-070/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-070/paulo-custodio/README b/challenge-070/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-070/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-070/paulo-custodio/perl/ch-1.pl b/challenge-070/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..fc3c102124
--- /dev/null
+++ b/challenge-070/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+
+# Challenge 070
+#
+# 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
+
+use Modern::Perl;
+
+my($S, $C, $O) = @ARGV;
+say swap($S, $C, $O);
+
+sub swap {
+ my($s, $c, $o) = @_;
+ for (1..$c) {
+ $s = swap1($s, $_, $o);
+ }
+ return $s;
+}
+
+sub swap1 {
+ my($s, $c, $o) = @_;
+ my @s = split //, $s;
+ my $p1 = $c % length($s);
+ my $p2 = ($c+$o) % length($s);
+ ($s[$p1], $s[$p2]) = ($s[$p2], $s[$p1]);
+ return join '', @s;
+}
diff --git a/challenge-070/paulo-custodio/perl/ch-2.pl b/challenge-070/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..c2873d5bbb
--- /dev/null
+++ b/challenge-070/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+
+# Challenge 070
+#
+# 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]
+
+use Modern::Perl;
+
+my $N = shift||2;
+say join(", ", gray($N));
+
+
+sub gray {
+ my($n) = @_;
+ if ($n < 2) { die; }
+ elsif ($n == 2) {
+ return (0, 1, 3, 2);
+ }
+ else {
+ # gray sequence of N-1
+ my @prev = gray($n-1);
+ # binary form to S1
+ my @s1 = map {sprintf("%0*b", $n-1, $_);} @prev;
+ # recerse to S2
+ my @s2 = reverse @s1;
+ # prexix S1 with 0
+ @s1 = map {"0".$_} @s1;
+ # prexix S2 with 1
+ @s2 = map {"1".$_} @s2;
+ # concatenate
+ my @gray = (@s1, @s2);
+ # convert to decimal
+ @gray = map {eval "0b$_"} @gray;
+
+ return @gray;
+ }
+}
diff --git a/challenge-070/paulo-custodio/t/test-1.yaml b/challenge-070/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..54b59fde28
--- /dev/null
+++ b/challenge-070/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: perlandraku 3 4
+ input:
+ output: pndraerlaku
diff --git a/challenge-070/paulo-custodio/t/test-2.yaml b/challenge-070/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..36b701bcbb
--- /dev/null
+++ b/challenge-070/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: 0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8