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-2.2.1.85 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.84 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.83 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.82 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.81 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.80 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.79 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.78 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.77 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.76 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.75 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.74 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.73 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.72 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.71 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.70 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.69 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.68 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.67 tracks/perl5/exercises/pascals-triangle/Example.pm
trackler-2.2.1.66 tracks/perl5/exercises/pascals-triangle/Example.pm