aboutsummaryrefslogtreecommitdiff
path: root/challenge-073/bob-lied/perl/MinSlidingWindow.pm
blob: 693aa931db91e8a40bbb2ea8ad1138216ee900a3 (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
32
33
34
35
36
37
38
39
40
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# ch-1.pl
#=============================================================================
# Copyright (c) 2020, Bob Lied
#=============================================================================
# Perl Weekly Challenge 073 Task #1 > Min Sliding Window
#=============================================================================
#
# You are given an array of integers @A and sliding window size $S.
# Write a script to create an array of min from each sliding window.

package MinSlidingWindow;

use strict;
use warnings;
use feature qw(say);

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(minSlidingWindow);
our @EXPORT_OK = qw();

use List::Util qw(min);

sub minSlidingWindow
{
    my ($aref, $win) = @_;
    my @result;

    for my $beg ( 0 .. scalar(@$aref) - $win )
    {
        my $min = List::Util::min( @{$aref}[$beg .. $beg+$win-1 ]);
        push @result, $min;
    }
    return \@result;
}

1;