Sha256: 128ca147ee324ede53913b83579a9357d290fb042f51e9465e34f9b1e4d770ef

Contents?: true

Size: 1.46 KB

Versions: 73

Compression:

Stored size: 1.46 KB

Contents

package Palindrome;

use strict;
use warnings;

sub new {
    my ($class, $min_max) = @_;
    my $self = {};
    $self->{max_factor} = $min_max->{max_factor};
    $self->{min_factor} = $min_max->{min_factor} || 1;
    return bless $self, $class;
}

sub largest {
    my $self = shift;
    return $self->_filter_factors( sub { $_[0] > $_[1] } );
}

sub smallest {
    my $self = shift;
    return $self->_filter_factors( sub { $_[0] < $_[1] } );
}

sub _filter_factors {
    my ($self, $filter) = @_;
    my @all_factors = @{ _palindrome_factors( $self->{min_factor}, $self->{max_factor} ) };
    my @factors = (shift @all_factors);
    my $extreme_value = $factors[0]->[0] * $factors[0]->[1];

    foreach my $f ( @all_factors ) {
        my $value = $f->[0] * $f->[1];

        if ($filter->($value, $extreme_value)) {
            @factors = ($f,);
            $extreme_value = $value;
            next;
        }
        push @factors, [$f->[0], $f->[1]] if $value == $extreme_value;
    }

    return { value => $extreme_value, factors => \@factors };
}

sub _palindrome_factors {
    my ($min, $max) = @_;
    my $jmin = $min;
    $max = $max;
    my @factors;
    
    foreach my $i ( $min..$max ) {
        foreach my $j ( $jmin..$max ) {
            my $value = $i * $j;
            push @factors, [$i, $j] if _is_palindrome($value);
        }
        $jmin++;
    }

    return [@factors];
}

sub _is_palindrome {
    my $input = shift;
    return $input eq scalar(reverse $input);
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.179 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.178 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.177 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.176 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.175 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.174 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.173 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.172 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.171 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.170 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.169 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.167 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.166 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.165 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.164 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.163 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.162 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.161 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm
trackler-2.2.1.160 tracks/perl5/exercises/palindrome-products/.meta/solutions/Palindrome.pm