From 1ff0e52d796f5ad1602bdf014df2650f98993217 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 27 Jul 2020 20:03:50 +0200 Subject: solution for task 1 --- challenge-071/jo-37/perl/ch-1.pl | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 challenge-071/jo-37/perl/ch-1.pl diff --git a/challenge-071/jo-37/perl/ch-1.pl b/challenge-071/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..c6a873d686 --- /dev/null +++ b/challenge-071/jo-37/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use List::Util qw(shuffle); + +# map3 is some kind of mixture and extension of map and reduce: +# It iterates over the elements of a list calling a code block, +# locally setting +# - $a to the previous element +# - $_ to the current element +# - $b to the next element +# and returning the the results of each invocation of the code block. +# The block is never called with $_ set to the first or last element +# of the list. +sub map3 (&@) { + my $code = shift; + + my ($prev, $current, $next, $i); + map { + ($prev, $current, $next) = ($current, $next, $_); + ++$i > 2 ? + do {local ($a, $_, $b) = ($prev, $current, $next); $code->()} : + (); + } @_; +} + +my $N = 10; +local $" = ', '; + +# Shuffle the deck and take the first N cards. +my @rand = splice @{[shuffle 1 .. 50]}, 0, $N; +print "Array: [@rand]\n"; + +# Add zeroes around the array to detect "border peaks". +print "Peak: [@{[map3 {$_ > $a && $_ > $b ? $_ : ()} 0, @rand, 0]}]\n"; -- cgit