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.139 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.138 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.137 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.136 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.135 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.134 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.133 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.132 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.131 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.130 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.129 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.128 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.127 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.126 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.125 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.124 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.123 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.122 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.121 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm
trackler-2.2.1.120 tracks/perl5/exercises/saddle-points/.meta/solutions/Matrix.pm