diff options
| author | 冯昶 <seaker@qq.com> | 2020-09-21 14:20:42 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2020-09-21 14:20:42 +0800 |
| commit | bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb (patch) | |
| tree | 877181cfde26b706346d3468269e4674d75da772 /challenge-078/abigail/Part1/solution.pl | |
| parent | ec09b571a6f2186fec8870a071a8d5d38596c850 (diff) | |
| parent | 5ac16ac7e9826137e0da5597e954f4992c66205d (diff) | |
| download | perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.gz perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.bz2 perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-078/abigail/Part1/solution.pl')
| -rwxr-xr-x | challenge-078/abigail/Part1/solution.pl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-078/abigail/Part1/solution.pl b/challenge-078/abigail/Part1/solution.pl new file mode 100755 index 0000000000..e74cbbb0c4 --- /dev/null +++ b/challenge-078/abigail/Part1/solution.pl @@ -0,0 +1,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; |
