How can I get all local extrema in Perl without my own?

This is not too difficult to implement, but I would prefer code reuse if possible.

my @arr = (2,3,4,5,5,5,4,4,3,1,1,2,3,0,2,4);
my $ret = {MAXIMA=>[{INDEX=>3, VAL=>5},{INDEX=>4, VAL=>5},{INDEX=>5, VAL=>5}],
           MINIMA=>[{INDEX=>9, VAL=>1},{INDEX=>10, VAL=>1},{INDEX=>13, VAL=>0}]}

So, do you know about any module that implements something similar?

+3
source share
3 answers

It could just be a gap in CPAN; he can use the local extrema module. Consider polishing and publishing!

Local maximum code (intentionally optimized for understanding, not efficiency):

Need help with peak detection in Perl

, , , : ? () , , ? k? ( n ), ? , .

+3

CPAN, , " X ": http://www.perlmonks.org/?node_id=629742 - " K " " "

+1

I do not know any CPAN modules that do this. However, for the starting point, run List::Util(main module) and List::MoreUtilsCPAN Modules to help you create the solution.

For instance,

use List::Util qw/min max/;
my $min = min @arr;
my $max = max @arr;

or

use List::MoreUtils ':all';
my ($min, $max) = minmax @arr;

# and then... 
my @maxima_indexes = indexes { $_ == $max } @arr;

# global maxima...
my @maxima = map { {INDEX => $_, VAL => $max} } @maxima_indexes;

/ I3az /

+1
source

Source: https://habr.com/ru/post/1768860/


All Articles