From df4aee15abcc46f8b2844b7ba035b45afa0ff31e Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 15 Sep 2020 13:26:46 +0200 Subject: Perl solutions for Week 78 --- challenge-078/abigail/Part1/solution.pl | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 challenge-078/abigail/Part1/solution.pl (limited to 'challenge-078/abigail/Part1/solution.pl') 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; -- cgit