aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-078/perlboy1967/perl/ch-1.pl33
-rwxr-xr-xchallenge-078/perlboy1967/perl/ch-2.pl34
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-078/perlboy1967/perl/ch-1.pl b/challenge-078/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..1e5fcdc1d5
--- /dev/null
+++ b/challenge-078/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 078
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-078/
+#
+# Task 1 - Leader Element
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use strict;
+use warnings;
+
+use List::Util qw(max);
+
+my @A = (9, 10, 7, 5, 6, 1);
+
+sub findLeaderElement {
+ my ($arL) = @_;
+
+ my $maxIdx = scalar(@$arL) - 1;
+
+ my %maxLoc;
+ my @max = map {
+ my $max = max(@$arL[$_ .. $maxIdx]);
+ push(@{$maxLoc{$max}}, $_);
+ $max;
+ } reverse (0 .. $maxIdx);
+
+ return @$arL[$maxLoc{max(keys %maxLoc)}[0] .. $maxIdx];
+}
+
+printf "Input: (%s)\n", join(', ', @A);
+printf "Output: (%s)\n", join(', ', findLeaderElement(\@A));
diff --git a/challenge-078/perlboy1967/perl/ch-2.pl b/challenge-078/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..97ac98e820
--- /dev/null
+++ b/challenge-078/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 078
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-078/
+#
+# Task 2 - Left Rotation
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use strict;
+use warnings;
+
+my @A = qw(7 4 2 6 3);
+my @B = qw(1 3 4);
+
+sub rotateArray {
+ my ($arL, $arR) = @_;
+
+ my @res;
+
+ my $maxIdx = scalar(@$arL) - 1 ;
+
+ foreach my $r (@$arR) {
+ $r %= ($maxIdx + 1);
+ push(@res, [@$arL[$r .. $maxIdx], @$arL[0 .. $r - 1]]);
+ }
+
+ return @res;
+}
+
+printf "Input:\n\t\@A = (%s)\n\t\@B = (%s)\n",
+ join(', ', @A), join(', ', @B);
+printf "Output:\n%s\n",
+ join("\n", map { "\t[".join(" ", @$_)."]" } rotateArray(\@A, \@B));