aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-15 18:10:19 +0100
committerGitHub <noreply@github.com>2020-09-15 18:10:19 +0100
commite01c732b9eba90983bf1528317bbd04bd5a1a5e3 (patch)
tree15ab206d2fa3b0227bc77de5ff62904911b2bae2
parent2ff5e85698bc8ef93bfc8b2d794b4164ddfd115d (diff)
parent51df58cae9fe169d327994c42f8e26f27d2d383f (diff)
downloadperlweeklychallenge-club-e01c732b9eba90983bf1528317bbd04bd5a1a5e3.tar.gz
perlweeklychallenge-club-e01c732b9eba90983bf1528317bbd04bd5a1a5e3.tar.bz2
perlweeklychallenge-club-e01c732b9eba90983bf1528317bbd04bd5a1a5e3.zip
Merge pull request #2303 from oWnOIzRi/week078
add solution week 78 task 1
-rw-r--r--challenge-078/steven-wilson/perl/ch-1.pl59
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-078/steven-wilson/perl/ch-1.pl b/challenge-078/steven-wilson/perl/ch-1.pl
new file mode 100644
index 0000000000..49cb5120a9
--- /dev/null
+++ b/challenge-078/steven-wilson/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+# 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 Test::More;
+use feature qw/ say /;
+
+my @E1I = ( 9, 10, 7, 5, 6, 1 );
+my @E1O = ( 10, 7, 6, 1 );
+my @E2I = ( 3, 4, 5 );
+my @E2O = (5);
+my @E3I = ();
+my @E3O = (0);
+
+is_deeply( get_leader_elements( \@E1I ), \@E1O );
+is_deeply( get_leader_elements( \@E2I ), \@E2O );
+is_deeply( get_leader_elements( \@E3I ), \@E3O );
+done_testing();
+
+sub get_leader_elements {
+ my $array_ref = shift;
+ my @array = @{$array_ref};
+ my @leader_elements;
+ say "Input: \@A = (", join( ", ", @array ), ")";
+ if ( scalar @array == 0 ) {
+ @leader_elements = (0);
+ }
+ else {
+ my $max = pop @array;
+ push @leader_elements, $max;
+ for ( reverse @array ) {
+ if ( $_ > $max ) {
+ push @leader_elements, $_;
+ $max = $_;
+ }
+ }
+ }
+ @leader_elements = reverse @leader_elements;
+ say "Output: (", join( ", ", @leader_elements ), ")";
+ return \@leader_elements;
+}