Sha256: 5b362443775862a0c5069b86adb4a49bde2c50fd9baac976e788ef5c3e5ad8f5

Contents?: true

Size: 709 Bytes

Versions: 323

Compression:

Stored size: 709 Bytes

Contents

package Example;

use strict;
use warnings;
use List::Util 'reduce';

sub new {
    my ($class, $input) = @_;
    return bless {input => $input}, $class;
}

sub slices {
    my ( $self, $size ) = @_;
    my $input = $self->{input};

    die "ArgumentError" if $size > length $input;

    return [ map { [ split // ] } $input =~ m/(?=(.{$size}))/g ];
}

sub largest_product {
    my ( $self, $size ) = @_;
    my @slices = @{ $self->slices($size) };

    return 1 if not $size;

    my $largest = reduce { $a * $b  } @{ shift @slices };
    foreach my $slice ( @slices ) {
        my $product = reduce { $a * $b } @$slice;
        $largest = $product if $product > $largest;
    }

    return $largest;
}

1;

Version data entries

323 entries across 323 versions & 1 rubygems

Version Path
trackler-2.2.1.105 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.104 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.103 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.102 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.101 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.100 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.99 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.98 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.97 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.96 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.95 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.94 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.93 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.92 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.91 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.90 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.89 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.88 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.87 tracks/perl5/exercises/largest-series-product/Example.pm
trackler-2.2.1.86 tracks/perl5/exercises/largest-series-product/Example.pm