aboutsummaryrefslogtreecommitdiff
path: root/challenge-070
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-07-24 16:36:21 +0100
committerGitHub <noreply@github.com>2020-07-24 16:36:21 +0100
commit17f7a707a8dbeee70c3ba93197dfce93b69db867 (patch)
tree1a267b3c4da21d603d8bfc91f6edf33971a4a5f4 /challenge-070
parent03a388d4f9d5f19957f1ca8d90f585c0c9bbe8b1 (diff)
parent1557cbe3c012d4484b8b6d3c00b09ca1e87f4e09 (diff)
downloadperlweeklychallenge-club-17f7a707a8dbeee70c3ba93197dfce93b69db867.tar.gz
perlweeklychallenge-club-17f7a707a8dbeee70c3ba93197dfce93b69db867.tar.bz2
perlweeklychallenge-club-17f7a707a8dbeee70c3ba93197dfce93b69db867.zip
Merge pull request #1976 from x1mandi/ch70
Solution for challenge-070 taks1 in Perl by x1mandi.
Diffstat (limited to 'challenge-070')
-rw-r--r--challenge-070/x1mandi/README1
-rw-r--r--challenge-070/x1mandi/perl/ch-1.pl44
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-070/x1mandi/README b/challenge-070/x1mandi/README
new file mode 100644
index 0000000000..f579b0b193
--- /dev/null
+++ b/challenge-070/x1mandi/README
@@ -0,0 +1 @@
+Solution by Csaba Simandi.
diff --git a/challenge-070/x1mandi/perl/ch-1.pl b/challenge-070/x1mandi/perl/ch-1.pl
new file mode 100644
index 0000000000..29bfe36675
--- /dev/null
+++ b/challenge-070/x1mandi/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use diagnostics;
+use utf8;
+use 5.26.0;
+#promt for a string from user (keyboard input)
+
+my $S = $ARGV[0] || "Perlmongers";
+my $C = $ARGV[1] || 3;
+my $O = $ARGV[2] || 4;
+
+print character_swapping($S, $C, $O)."\n";
+
+sub character_swapping {
+ my ($s, $c, $o) = @_;
+ my $N = length($s);
+
+ die "ERROR: Missing string.\n" unless defined $s;
+ die "ERROR: Missing swap count.\n" unless defined $c;
+ die "ERROR: Missing offset.\n" unless defined $o;
+ die "ERROR: Invalid swap count [$c].\n" unless ( $c >= 1 );
+ die "ERROR: Invalid offset [$o].\n" unless ( $o >= 1 );
+ #Why swap count cannot be greater that offset? Checked $S=abcd, $C=2, $O=1 and the output is "bcda"
+ die "ERROR: Swap count [$c] is greater than offset [$o].\n" unless ( $c <= $o );
+ # When $N = $c-1
+ die "ERROR: Invalid string length [$N] for given swap count and offset [$c + $o].\n"
+ unless ( ($c + $o) <= $N);
+
+ my @chars = split //, $s;
+
+ #Why is swapping not applied to the first character at index 0?
+ for (1..$c) {
+ my $firstchar = $chars[$_ % $N];
+ my $secondchar = $chars[ ($_+$o) % $N];
+
+ $chars[($_+$o) % $N] = $firstchar;
+ $chars[$_ % $N] = $secondchar;
+ }
+
+ $s = join('', @chars);
+ return $s;
+} \ No newline at end of file