aboutsummaryrefslogtreecommitdiff
path: root/challenge-078
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-09-16 01:50:14 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-09-16 01:50:14 +0100
commitb7c69245e0ad553494a646a2ea9b93f8f8ccd35d (patch)
treed37a243d19b8614f703c3e9401c33d03e6c18ce0 /challenge-078
parent725b4c475f3a4c80ac89692dc6143c983f73783e (diff)
downloadperlweeklychallenge-club-b7c69245e0ad553494a646a2ea9b93f8f8ccd35d.tar.gz
perlweeklychallenge-club-b7c69245e0ad553494a646a2ea9b93f8f8ccd35d.tar.bz2
perlweeklychallenge-club-b7c69245e0ad553494a646a2ea9b93f8f8ccd35d.zip
- Added Raku solution to "Left Rotation" task.
Diffstat (limited to 'challenge-078')
-rw-r--r--challenge-078/mohammad-anwar/raku/ch-2.raku47
-rw-r--r--challenge-078/mohammad-anwar/raku/ch-2.t53
2 files changed, 100 insertions, 0 deletions
diff --git a/challenge-078/mohammad-anwar/raku/ch-2.raku b/challenge-078/mohammad-anwar/raku/ch-2.raku
new file mode 100644
index 0000000000..85c0cbc761
--- /dev/null
+++ b/challenge-078/mohammad-anwar/raku/ch-2.raku
@@ -0,0 +1,47 @@
+#!/usr/bin/env raku
+
+#
+# Perl Weekly Challenge - 078
+#
+# Task #2: Left Rotation
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-078
+#
+
+use v6.d;
+
+sub MAIN(Str :$A = "10, 20, 30, 40, 50", Str :$B = "3, 4") {
+ left-rotation($A, $B).join("\n").say;
+}
+
+#
+#
+# METHOD
+
+sub left-rotation(Str $source, Str $index) {
+
+ die "ERROR: Invalid source [$source].\n"
+ unless $source ~~ /^[\-?\d\,?\s?]+$/;
+ die "ERROR: Invalid index [$index].\n"
+ unless $index ~~ /^[\-?\d\,?\s?]+$/;
+
+ my @source = $source.split(',').map: { .Int };
+ my @index = $index.split(',').map: { .Int };
+
+ my @left-rotation = Empty;
+ for @index -> $i {
+ my @array = Empty;
+ for $i..@source.elems-1 -> $j {
+ @array.push: @source.[$j];
+ }
+ if $i > 0 {
+ for 0..$i-1 -> $k {
+ @array.push: @source.[$k];
+ }
+ }
+
+ @left-rotation.push: @array;
+ }
+
+ return |@left-rotation;
+}
diff --git a/challenge-078/mohammad-anwar/raku/ch-2.t b/challenge-078/mohammad-anwar/raku/ch-2.t
new file mode 100644
index 0000000000..6fc555f1a0
--- /dev/null
+++ b/challenge-078/mohammad-anwar/raku/ch-2.t
@@ -0,0 +1,53 @@
+#!/usr/bin/env raku
+
+#
+# Perl Weekly Challenge - 078
+#
+# Task #2: Left Rotation
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-078
+#
+
+use Test;
+
+is-deeply [left-rotation("10, 20, 30, 40, 50", "3, 4")],
+ [[40, 50, 10, 20, 30], [50, 10, 20, 30, 40]],
+ "testing example 1";
+
+is-deeply [left-rotation("7, 4, 2, 6, 3", "1, 3, 4")],
+ [[4, 2, 6, 3, 7], [6, 3, 7, 4, 2], [3, 7, 4, 2, 6]],
+ "testing example 2";
+
+done-testing;
+
+#
+#
+# METHOD
+
+sub left-rotation(Str $source, Str $index) {
+
+ die "ERROR: Invalid source [$source].\n"
+ unless $source ~~ /^[\-?\d\,?\s?]+$/;
+ die "ERROR: Invalid index [$index].\n"
+ unless $index ~~ /^[\-?\d\,?\s?]+$/;
+
+ my @source = $source.split(',').map: { .Int };
+ my @index = $index.split(',').map: { .Int };
+
+ my @left-rotation = Empty;
+ for @index -> $i {
+ my @array = Empty;
+ for $i..@source.elems-1 -> $j {
+ @array.push: @source.[$j];
+ }
+ if $i > 0 {
+ for 0..$i-1 -> $k {
+ @array.push: @source.[$k];
+ }
+ }
+
+ @left-rotation.push: @array;
+ }
+
+ return |@left-rotation;
+}