#Perl Weekly Challenge #73
Tags: perl
Published: 2020-08-19 08:07:55

---
title: #Perl Weekly Challenge #73
published: true
date: 2020-08-16 13:45:38 UTC
tags: perl
canonical_url: https://lancewicks.com/2020/08/16/perl-weekly-challenge-73/#utm_source=rss&utm_medium=rss
---

Recently, I have been participating in Mohammad Anwar’s [Perl Weekly Challenge](https://perlweeklychallenge.org/). Last week I solve in Perl and Elm… and live streamed it via [Twitch](https://www.twitch.tv/videos/702391523) and uploaded to [Youtube](https://www.youtube.com/watch?v=OA7dCY7vDJo).

This week, despite not feeling great I took a stab at the “Min Sliding Window” challenge over at [https://perlweeklychallenge.org/blog/perl-weekly-challenge-073/](https://perlweeklychallenge.org/blog/perl-weekly-challenge-073/) in #Perl.

I try and do these challenges in a TDD(ish) style. This week I did not do great; not feeling 100% well I cut some corners and spun my wheels quite a bit. However, I got there in the end.

## Getting started.

I’ve used challenges like this as opportunities to keep my coding skills from getting too rusty quite a bit over time so I have a “methodology” I tend to use.

I start with a [Test2::V0](https://metacpan.org/pod/Test2::V0) test file which starts off looking like this:

```
use Test2::V0 -target => 'Sliding';

done_testing;
```

This fails straight out of the box, as the library does not exist yet. So I create a .pm file:

```
package Sliding;
 
1;
```

From then, I start building out tests; then library in the traditional Red/Green loop. And thats how I do it; and it looks like this:

```
use Test2::V0 -target => 'Sliding';

subtest 'min_from_windows example' => sub {
    my @array = ( 1, 5, 0, 2, 9, 3, 7, 6, 4, 8 );
    my $window_size = 3;
    my @expected = ( 0, 0, 0, 2, 3, 3, 4, 4 );

    is $CLASS->min_from_windows(
        array_of_numbers => \@array,
        window_size => $window_size,
        ),
        \@expected;
};

subtest 'min_from_windows test two' => sub {
    my @array = ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, );
    my $window_size = 3;
    my @expected = ( 0, 1, 2, 3, 4, 5, 6, 7 );

    is $CLASS->min_from_windows(
        array_of_numbers => \@array,
        window_size => $window_size,
        ),
        \@expected;

};

subtest 'min_from_windows test three' => sub {
    my @array = ( 0, 1, 2, 3, 4, 5 );
    my $window_size = 2;
    my @expected = ( 0, 1, 2, 3, 4 );

    is $CLASS->min_from_windows(
        array_of_numbers => \@array,
        window_size => $window_size,
        ),
        \@expected;

};

subtest 'min_from_windows test three' => sub {
    my @array = ( 99, 1, 32, 4, 5, 5 );
    my $window_size = 2;
    my @expected = ( 1, 1, 4, 4, 5 );

    is $CLASS->min_from_windows(
        array_of_numbers => \@array,
        window_size => $window_size,
        ),
        \@expected;

};
done_testing;

```

And the library looks like this:

```
package Sliding;
  
use List::Util 'min';

sub min_from_windows {
    my ($self, %args) = @_;
    my @numbers = @{$args{array_of_numbers}};
    my $length = @numbers;

    my $the_end = $length - $args{window_size};

    my @array_of_mins;
    for my $i (0 .. $the_end){
        my @slice = @numbers[$i .. ($i-1) + $args{window_size}];

        push @array_of_mins, min @slice;

    }

    return \@array_of_mins;
}

1;

```

As I have said to Mohammad on the stream a few times; I am not doing this as well as potentially I could/should. For example I have not validated my inputs. In this case I’ve not even turned it into a script as per the instructions.

Participating is not restricted to Perl; as I said I have done it in Elm. So, even if you are not a Perl developer you can give it a go.

Just pop over to [https://perlweeklychallenge.org](https://perlweeklychallenge.org) and see what is required and give it a shot.

Lance