Sha256: 4ab1ac6f58046c12b89fdc8c6d1ea9fae71a505ea2e2309110492adb513f9fb2

Contents?: true

Size: 1.58 KB

Versions: 73

Compression:

Stored size: 1.58 KB

Contents

package Triplet;

use strict;
use warnings;

sub new {
    my ($class, @args) = @_;
    my $self = bless {}, $class;

    # if hash is passed
    foreach my $arg (@args) {
        if(ref $arg eq 'HASH') {
            foreach my $key (keys %$arg) {
                $self->{$key} = $arg->{$key};
            }
            return $self;
        }
    }

    # a, b, c is passed
    ($self->{a}, $self->{b}, $self->{c}) = @args;

    return $self;
}

sub is_pythagorean {
    my $self = shift;
    return $self->{a}**2 + $self->{b}**2 == $self->{c}**2;
}

sub sum {
    my $self = shift;
    return $self->{a} + $self->{b} + $self->{c};
}

sub product {
    my $self = shift;

    if ($self->is_pythagorean) {
        return $self->{a} * $self->{b} * $self->{c};
    }
    return 0;
}

sub products {
    my $self = shift;

    my $min = $self->{min_factor} || 1;
    my $max = $self->{max_factor} || 1;
    my $sum = $self->{sum}        || 0;
    my @triplets;
    my @product_triplets;

    foreach my $i ($min..$max) {
        my @pytha_triplets;
        foreach my $j($i..$max) {
            foreach my $k($j..$max) {
                my $temp_triplet = __PACKAGE__->new($i, $j, $k);
                push @pytha_triplets, $temp_triplet if $temp_triplet->is_pythagorean;
            }
        }
        push @triplets, @pytha_triplets;
    }

    if ($self->{sum}) {
        foreach my $triplet (@triplets) {
            push @product_triplets, $triplet if $self->{sum} == $triplet->sum;
        }
    } else {
        @product_triplets = @triplets;
    }

    return [ map { $_->product } @product_triplets ];
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

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