DEV Community

Lance Wicks
Lance Wicks

Posted on • Originally published at lancewicks.com on

#Perl Weekly Challenge #73

Recently, I have been participating in Mohammad Anwar’s Perl Weekly Challenge. Last week I solve in Perl and Elm… and live streamed it via Twitch and uploaded to Youtube.

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/ 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 test file which starts off looking like this:

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

done_testing;
Enter fullscreen mode Exit fullscreen mode

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

package Sliding;

1;
Enter fullscreen mode Exit fullscreen mode

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;

Enter fullscreen mode Exit fullscreen mode

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;

Enter fullscreen mode Exit fullscreen mode

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 and see what is required and give it a shot.

Lance

Top comments (0)