aboutsummaryrefslogtreecommitdiff
path: root/challenge-078
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2020-12-27 10:39:02 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2020-12-27 10:39:02 +0000
commitf7aa30feb92baeb3c6c3586c5ea49c393fcbc557 (patch)
tree158606a61c20255605f7ca0b01bb5ddaac84120d /challenge-078
parent8b82802e962f27d327dd7c2d8bb14b96b2ceec4d (diff)
downloadperlweeklychallenge-club-f7aa30feb92baeb3c6c3586c5ea49c393fcbc557.tar.gz
perlweeklychallenge-club-f7aa30feb92baeb3c6c3586c5ea49c393fcbc557.tar.bz2
perlweeklychallenge-club-f7aa30feb92baeb3c6c3586c5ea49c393fcbc557.zip
Add Perl solution to challenge 078
Diffstat (limited to 'challenge-078')
-rw-r--r--challenge-078/paulo-custodio/README1
-rw-r--r--challenge-078/paulo-custodio/perl/ch-1.pl34
-rw-r--r--challenge-078/paulo-custodio/perl/ch-2.pl56
-rw-r--r--challenge-078/paulo-custodio/test.pl39
4 files changed, 130 insertions, 0 deletions
diff --git a/challenge-078/paulo-custodio/README b/challenge-078/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-078/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-078/paulo-custodio/perl/ch-1.pl b/challenge-078/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..f92e0e43d8
--- /dev/null
+++ b/challenge-078/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+# Challenge 078
+#
+# TASK #1 › Leader Element
+# Submitted by: Mohammad S Anwar
+# You are given an array @A containing distinct integers.
+#
+# Write a script to find all leader elements in the array @A. Print (0) if none found.
+#
+# An element is leader if it is greater than all the elements to its right side.
+#
+# Example 1:
+# Input: @A = (9, 10, 7, 5, 6, 1)
+# Output: (10, 7, 6, 1)
+# Example 2:
+# Input: @A = (3, 4, 5)
+# Output: (5)
+
+use strict;
+use warnings;
+use 5.030;
+
+my @A = @ARGV;
+my $max = 0;
+my @leaders;
+for my $i (reverse 0..$#A) {
+ if ($A[$i] > $max) {
+ unshift @leaders, $A[$i];
+ $max = $A[$i];
+ }
+}
+@leaders = (0) unless @leaders;
+say "(", join(", ", @leaders), ")";
diff --git a/challenge-078/paulo-custodio/perl/ch-2.pl b/challenge-078/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..123fd31541
--- /dev/null
+++ b/challenge-078/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+
+# Challenge 078
+#
+# TASK #2 › Left Rotation
+# Submitted by: Mohammad S Anwar
+# You are given array @A containing positive numbers and @B containing one or more indices from the array @A.
+#
+# Write a script to left rotate @A so that the number at the first index of @B becomes the first element in the array.
+# Similarly, left rotate @A again so that the number at the second index of @B becomes the first element in the array.
+#
+# Example 1:
+# Input:
+# @A = (10 20 30 40 50)
+# @B = (3 4)
+# Explanation:
+# a) We left rotate the 3rd index element (40) in the @A to make it 0th index member in the array.
+# [40 50 10 20 30]
+#
+# b) We left rotate the 4th index element (50) in the @A to make it 0th index member in the array.
+# [50 10 20 30 40]
+#
+# Output:
+# [40 50 10 20 30]
+# [50 10 20 30 40]
+# Example 2:
+# Input:
+# @A = (7 4 2 6 3)
+# @B = (1 3 4)
+# Explanation:
+# a) We left rotate the 1st index element (4) in the @A to make it 0th index member in the array.
+# [4 2 6 3 7]
+#
+# b) We left rotate the 3rd index element (6) in the @A to make it 0th index member in the array.
+# [6 3 7 4 2]
+#
+# c) We left rotate the 4th index element (3) in the @A to make it 0th index member in the array.
+# [3 7 4 2 6]
+#
+# Output:
+# [4 2 6 3 7]
+# [6 3 7 4 2]
+# [3 7 4 2 6]
+
+use strict;
+use warnings;
+use 5.030;
+
+my($A, $B) = @ARGV;
+my @A = split ' ', $A;
+my @B = split ' ', $B;
+
+for my $i (@B) {
+ my @a = (@A[$i .. $#A], @A[0 .. $i-1]);
+ say "[@a]";
+}
diff --git a/challenge-078/paulo-custodio/test.pl b/challenge-078/paulo-custodio/test.pl
new file mode 100644
index 0000000000..fa5ba02461
--- /dev/null
+++ b/challenge-078/paulo-custodio/test.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+use 5.030;
+
+is capture("perl perl/ch-1.pl 9 10 7 5 6 1"), "(10, 7, 6, 1)\n";
+is capture("perl perl/ch-1.pl 3 4 5"), "(5)\n";
+is capture("perl perl/ch-1.pl "), "(0)\n";
+is capture("perl perl/ch-1.pl 0 0 0 0 0"), "(0)\n";
+
+is capture("perl perl/ch-2.pl '10 20 30 40 50' '3 4'"), <<END;
+[40 50 10 20 30]
+[50 10 20 30 40]
+END
+
+is capture("perl perl/ch-2.pl '7 4 2 6 3' '1 3 4'"), <<END;
+[4 2 6 3 7]
+[6 3 7 4 2]
+[3 7 4 2 6]
+END
+
+is capture("perl perl/ch-2.pl '7 4 2 6 3' '0 1 2 3 4'"), <<END;
+[7 4 2 6 3]
+[4 2 6 3 7]
+[2 6 3 7 4]
+[6 3 7 4 2]
+[3 7 4 2 6]
+END
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+}