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)


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) {
    } else {
    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.

Add A Comment

Your Name
Your Email
Your Comment

Your submission will be ignored if any field is left blank, but your email address will not be displayed. Comments will be processed through markdown.

Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech beer boardgaming book of the week bookmonth chain of command children chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 cycling dead of winter doctor who documentary drama driving drone ecchi economics espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo geodata gin gurps gurps 101 harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo-nebula reread humour in brief avoid instrumented life kickstarter learn to play leaving earth linux lovecraftiana mecha men with beards museum mystery naval non-fiction one for the brow opera perl perl weekly challenge photography podcast politics 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 science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance 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