aboutsummaryrefslogtreecommitdiff
path: root/challenge-078
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-14 08:21:52 +0100
committerGitHub <noreply@github.com>2020-09-14 08:21:52 +0100
commit96607ebd36d0498f294b4e4b93ca9eeb7cd6612b (patch)
tree3512383adfcafaa851d5f119cdab12933a72d4b6 /challenge-078
parent7c96f879d98447f886b505bc47573231f3eb89f3 (diff)
parent6a6abb02362fcfbf45a834b59cf26369d77be6c8 (diff)
downloadperlweeklychallenge-club-96607ebd36d0498f294b4e4b93ca9eeb7cd6612b.tar.gz
perlweeklychallenge-club-96607ebd36d0498f294b4e4b93ca9eeb7cd6612b.tar.bz2
perlweeklychallenge-club-96607ebd36d0498f294b4e4b93ca9eeb7cd6612b.zip
Merge pull request #2282 from ccntrq/challenge-078
Challenge 078
Diffstat (limited to 'challenge-078')
-rw-r--r--challenge-078/alexander-pankoff/perl/ch-1.pl39
-rw-r--r--challenge-078/alexander-pankoff/perl/ch-2.pl32
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-078/alexander-pankoff/perl/ch-1.pl b/challenge-078/alexander-pankoff/perl/ch-1.pl
new file mode 100644
index 0000000000..9f4a8f1b5c
--- /dev/null
+++ b/challenge-078/alexander-pankoff/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+use v5.20;
+use utf8;
+use strict;
+use warnings;
+use autodie;
+use feature qw(say signatures);
+no warnings 'experimental::signatures';
+
+use List::Util qw(all reduce);
+
+# 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.
+
+my @A = @ARGV;
+
+my @leaders = find_leader_elements(@A);
+@leaders = (0) unless @leaders;
+
+say '(' . join( ', ', @leaders ) . ')';
+
+exit;
+
+sub find_leader_elements(@list) {
+ my $leaders = reduce {
+ my @leaders = @$a;
+ my $current = $b;
+
+ ( all { $current > $_ } @leaders ) ? [ $current, @leaders ] : $a
+ }
+ [], reverse @list;
+
+ return @{$leaders};
+}
diff --git a/challenge-078/alexander-pankoff/perl/ch-2.pl b/challenge-078/alexander-pankoff/perl/ch-2.pl
new file mode 100644
index 0000000000..f4dd950b48
--- /dev/null
+++ b/challenge-078/alexander-pankoff/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+use v5.20;
+use utf8;
+use strict;
+use warnings;
+use autodie;
+use feature qw(say signatures);
+no warnings 'experimental::signatures';
+
+# 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. Similary, left rotate @A again so
+# that the number at the second index of @B becomes the first element in the
+# array.
+
+my @A = ( 10, 20, 30, 40, 50 );
+my @B = ( 3, 4 );
+
+for my $index (@B) {
+ say '[' . join( ', ', left_rotate( $index, @A ) ) . ']';
+}
+
+sub left_rotate ( $index, @array ) {
+ while ( $index-- ) {
+ my $top = shift @array;
+ push @array, $top;
+ }
+
+ return @array;
+}