aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWalt Mankowski <waltman@pobox.com>2020-07-20 20:25:00 -0400
committerWalt Mankowski <waltman@pobox.com>2020-07-20 20:25:00 -0400
commit0461d18d6e324d7a53c2eca9af7c287b48dc23a5 (patch)
tree6f8c18b4090e63a648ffaabcd0b48b67cb415ebd
parentd96c3b3776fbb99ff873a07312075e0d34ea77c1 (diff)
downloadperlweeklychallenge-club-0461d18d6e324d7a53c2eca9af7c287b48dc23a5.tar.gz
perlweeklychallenge-club-0461d18d6e324d7a53c2eca9af7c287b48dc23a5.tar.bz2
perlweeklychallenge-club-0461d18d6e324d7a53c2eca9af7c287b48dc23a5.zip
perl code for challenge 70 task 1
-rw-r--r--challenge-070/walt-mankowski/perl/ch-1.pl51
1 files changed, 51 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;