Sha256: 3c03fbb922245899d8354603b382f00233725346487e518992d1a63c734ae5ed

Contents?: true

Size: 661 Bytes

Versions: 323

Compression:

Stored size: 661 Bytes

Contents

package Example;

use strict;
use warnings;
use 5.010; # use smart_match ~~

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

323 entries across 323 versions & 1 rubygems

Version Path
trackler-1.0.1.0 tracks/perl5/pascals-triangle/Example.pm
trackler-1.0.0.1 tracks/perl5/pascals-triangle/Example.pm
trackler-1.0.0 tracks/perl5/pascals-triangle/Example.pm