aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/andrezgz/perl5/ch-2.pl
blob: 3d9d8e208ab4e57d51ab8ab5ebc812a4e8e017a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/perl

# https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/
# Challenge #2
# Create a script that generates Pascal Triangle. Accept number of rows from the command line.
# The Pascal Triangle should have at least 3 rows.
# For more information about Pascal Triangle, check this wikipedia page. https://en.wikipedia.org/wiki/Pascal%27s_triangle

use strict;
use warnings;

my $rows = $ARGV[0] || 0;

if ($rows < 3) {
    print "The Pascal Triangle should have at least 3 rows\n";
    $rows = 3;
}

my @row = (1);

while ($rows) {
    print join(' ',@row) .$/;
    $rows--;
    @row = map {$row[$_] += $row[$_+1]} 0..@row-2;
    unshift @row, 1;
    push @row, 1;
}