# RogerBW's Blog

Perl Weekly Challenge 73: fun with minima 15 August 2020

I’ve been doing the Perl Weekly Challenges. The latest involved finding minima in subsets of lists.

# 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. 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)

(etc.)

So the first thing I wanted was a fast minimum-finding function:

``````use List::Util qw(min);
``````

(More people should use `List::Util`; it's great.)

Then it's just a matter of array-slicing the input and storing the result. No point in using a ring buffer for something this small.

``````sub msw {
my \$a=shift;
my \$s=shift;
my @out;
foreach my \$i (0..(scalar @{\$a})-\$s) {
push @out,min(@{\$a}[\$i..\$i+\$s-1]);
}
return \@out;
}
``````

Raku is the same except for the usual array fiddliness.

``````sub msw(@a,\$s) {
my @out;
for (0..(@a.elems-\$s)) -> \$i {
push @out,min(@a[\$i..\$i+\$s-1]);
}
return @out.flat;
}
``````

# 2. Smallest Neighbour

You are given an array of integers `@A`.

Write a script to create an array that represents the smaller element to the left of each corresponding index. If none found then use 0.

Er, what?

But the examples made this clearer: "For each element N, if any element to the left of N is smaller than N, return the smallest of them; otherwise, return 0".

I'd love to know why one might want to do this (as with mystery stories, I like some narrative with my puzzle).

Anyway. `List::Util` again, obviously. At each step through the list we update the minimum value of all-entries-to-the-left (unlike problem we never remove anything from consideration so the minimum can only get lower), then push either that or zero onto the output. We short-circuit the first step since it's always zero.

``````sub sn {
my \$a=shift;
my @out=(0);
my \$wm;
foreach my \$i (1..\$#{\$a}) {
if (!defined \$wm) {
\$wm=\$a->[\$i-1];
} else {
\$wm=min(\$wm,\$a->[\$i-1]);
}
if (\$wm < \$a->[\$i]) {
push @out,\$wm;
} else {
push @out,0;
}
}
return \@out;
}
``````

And Raku is mostly identical.

1. Posted by Peter C at 09:21am on 15 August 2020

I am amazed that you are still persisting with these. The puzzle-setter clearly ran out of ideas some while back, and the descriptions are badly-written and/or English is not their first language.

2. Posted by RogerBW at 09:52am on 15 August 2020

There are some interesting strangenesses in them; it's a good way to wake up on a Monday morning; and if the problem's an easy one, I try to do it with some style.

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1