aboutsummaryrefslogtreecommitdiff
path: root/challenge-063
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-02 14:51:58 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-02 14:51:58 +0100
commit847e53be79fd29d0be9c0aeabd3be3a84410f495 (patch)
tree627ff7d996ef27be193b50a51a27986704ca028c /challenge-063
parent15b9c017f65e331a99096167214fea292f6648ae (diff)
downloadperlweeklychallenge-club-847e53be79fd29d0be9c0aeabd3be3a84410f495.tar.gz
perlweeklychallenge-club-847e53be79fd29d0be9c0aeabd3be3a84410f495.tar.bz2
perlweeklychallenge-club-847e53be79fd29d0be9c0aeabd3be3a84410f495.zip
- Added Perl solutions to the "Rotate String" task.
Diffstat (limited to 'challenge-063')
-rw-r--r--challenge-063/mohammad-anwar/perl/ch-2.pl33
-rw-r--r--challenge-063/mohammad-anwar/perl/ch-2a.pl37
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-063/mohammad-anwar/perl/ch-2.pl b/challenge-063/mohammad-anwar/perl/ch-2.pl
new file mode 100644
index 0000000000..846db32d2a
--- /dev/null
+++ b/challenge-063/mohammad-anwar/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my $count = rotate($ARGV[0], 1);
+print "Rotation: $count\n";
+
+sub rotate {
+ my ($string, $verbose) = @_;
+
+ die "ERROR: Missing string.\n"
+ unless defined $string;
+ die "ERROR: Invalid string [$string].\n"
+ unless ($string =~ /^[xy]+$/i);
+
+ my $size = length($string);
+ my $temp = $string;
+ my $i = 1;
+ my $c = 1;
+ while ($i <= $size) {
+ my $part_a = substr($temp, 0, $i);
+ my $part_b = substr($temp, $i);
+ $temp = sprintf("%s%s", $part_b, $part_a);
+ print "[$c]: [$temp]\n" if ($verbose);
+ last if ($temp eq $string);
+
+ $i++; $c++;
+ $i = 1 if ($i > $size);
+ }
+
+ return $c;
+}
diff --git a/challenge-063/mohammad-anwar/perl/ch-2a.pl b/challenge-063/mohammad-anwar/perl/ch-2a.pl
new file mode 100644
index 0000000000..d51180d150
--- /dev/null
+++ b/challenge-063/mohammad-anwar/perl/ch-2a.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+is (rotate('xyxx'), 7, 'xyxx');
+is (rotate('xyxy'), 3, 'xyxy');
+
+done_testing;
+
+sub rotate {
+ my ($string, $verbose) = @_;
+
+ die "ERROR: Missing string.\n"
+ unless defined $string;
+ die "ERROR: Invalid string [$string].\n"
+ unless ($string =~ /^[xy]+$/i);
+
+ my $size = length($string);
+ my $temp = $string;
+ my $i = 1;
+ my $c = 1;
+ while ($i <= $size) {
+ my $part_a = substr($temp, 0, $i);
+ my $part_b = substr($temp, $i);
+ $temp = sprintf("%s%s", $part_b, $part_a);
+ print "[$c]: [$temp]\n" if ($verbose);
+ last if ($temp eq $string);
+
+ $i++; $c++;
+ $i = 1 if ($i > $size);
+ }
+
+ return $c;
+}