aboutsummaryrefslogtreecommitdiff
path: root/challenge-073/noud/raku/ch-1.p6
blob: c2a9cdf67e6173e79dd91007c30c9c07a78596f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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.
# Example
# Input: @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8) and $S = 3
# Output: (0, 0, 0, 2, 3, 3, 4, 4)
#
# [(1 5 0) 2 9 3 7 6 4 8] = Min (0)
# [1 (5 0 2) 9 3 7 6 4 8] = Min (0)
# [1 5 (0 2 9) 3 7 6 4 8] = Min (0)
# [1 5 0 (2 9 3) 7 6 4 8] = Min (2)
# [1 5 0 2 (9 3 7) 6 4 8] = Min (3)
# [1 5 0 2 9 (3 7 6) 4 8] = Min (3)
# [1 5 0 2 9 3 (7 6 4) 8] = Min (4)
# [1 5 0 2 9 3 7 (6 4 8)] = Min (4)

sub min-sliding-window(@A, $S) {
    gather for 0..@A.elems-$S -> $i {
        take min(@A[$i..$i+$S-1]);
    }
}

min-sliding-window((1, 5, 0, 2, 9, 3, 7, 6, 4, 8), 3).say;