aboutsummaryrefslogtreecommitdiff
path: root/challenge-078/abigail/Part1/solution.pl
blob: e74cbbb0c4ec41478fdb204453120626e15715eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/opt/perl/bin/perl

#
# Exercise:
#    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.
#

#
# Note:
#    - The only way no leader element can be found is if the array is empty.
#    - We will read the array from STDIN.
#

use 5.032;

use strict;
use warnings;
no  warnings 'syntax';

use experimental 'signatures';

my $max;

#
# Read the input, extract integers, and store them in @A.
# If the input is empty, print 0 and exit.
#
say (0), exit unless my @A = <> =~ /[0-9]+/g;

local $, = " ";

#
# Reverse the array, and extract each element which was larger
# than any seen before (keep state in $max), then reverse it
# again before printing.
#

say reverse
    grep {!defined $max || $_ > $max ? do {$max = $_; 1} : 0}
    reverse @A;