Sha256: 7c1e4f93ff5860414daa4ffc4bb8e46c8b6fbd73262cbecbc318ab58f7c1e9cc

Contents?: true

Size: 1.09 KB

Versions: 73

Compression:

Stored size: 1.09 KB

Contents

package Matrix;

use strict;
use warnings;

sub new {
    my ($class, $matrix_str) = @_;
    my $self = bless {}, $class;
    my @rows = map { [split /\s/, $_] }  split /\n/, $matrix_str;

    foreach my $i ( 0..$#{$rows[0]} ) {
        my @row;
        foreach my $j ( 0..$#rows ) {
            push @row, $rows[$j]->[$i];
        }
        push @{$self->{columns}}, \@row;
    }
    $self->{rows} = \@rows;

    return $self;
}

sub rows {
    my ($self, $index) = @_;
    return $self->{rows}->[$index];
}

sub columns {
    my ($self, $index) = @_;
    return $self->{columns}->[$index];
}

sub saddle_points {
    my $self = shift;
    my @points;

    foreach my $i ( 0..$#{$self->{rows}} ) {
        foreach my $j ( 0..$#{$self->{rows}->[$i]} ) {

            my @row = @{ $self->{rows}->[$i] };
            my $current = splice @row, $j, 1;
            next if grep { $current < $_ } @row;

            my @column = @{ $self->columns($j) };
            splice @column, $i, 1;
            next if grep { $current > $_ } @column;

            push @points => [$i, $j];

        }
    }

    return \@points;
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

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