Sha256: 85b775fc4eacb156c516aab90d258261b0b22b1d04863f42f530a2770e0c64cc

Contents?: true

Size: 709 Bytes

Versions: 49

Compression:

Stored size: 709 Bytes

Contents

package Triangle;

use strict;
use warnings;
use 5.010; # use smart_match ~~
no if $] >= 5.018, warnings => 'experimental';

sub triangle {
    my $size = shift;
    my @rows = ([1],);

    foreach my $i (1..$size) {
       my ($prev, $next) = $rows[$i-1];
       for my $j (0..@$prev) {
           $next->[$j] += $prev->[$j-1] if $j > 0;
           $next->[$j] += $prev->[$j]   if $j < @$prev;
       }
       push @rows => $next;
    }
    return [ map { join q{ } => @$_ } @rows ];
}

sub is_triangle {
    my $aaa = shift;
    my $bbb = triangle(@$aaa-1);

    return int(@$aaa ~~ @$bbb); # JSON::true is 1 JSON::false is 0
}

sub row {
    my $n = shift;
    return (triangle($n))->[$n];
}

__PACKAGE__;

Version data entries

49 entries across 49 versions & 1 rubygems

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