aboutsummaryrefslogtreecommitdiff
path: root/challenge-078/sgreen/perl/ch-1.pl
blob: e909010d15e6b6c322d57eb4cbebc14813654cd8 (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
#!/usr/bin/env perl

use strict;
use warnings;
use feature qw(say);
use List::Util qw(max);

sub main {
    my @array  = @_;
    my @leader = ();

    # Sanity checks
    die "You must specify one or more integers\n" unless scalar @array;
    foreach my $value (@array) {
        die "The value '$value' is not an integer\n"
          unless $value =~ /^-?[0-9]+$/;
    }

    for my $index ( 0 .. $#array - 1 ) {
        # Get the current value, and the maximum of the remaining values
        my $value = $array[$index];
        my $max   = max @array[ $index + 1 .. $#array ];
        push @leader, $value if $value > $max;
    }

    # The last value is always a maximum
    push @leader, $array[-1];
    say '(', join( ', ', @leader ), ')';
}

main(@ARGV);