aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorboblied <boblied@gmail.com>2021-11-14 19:27:38 -0600
committerboblied <boblied@gmail.com>2021-11-14 19:27:38 -0600
commit17fd72285fae20e3f79d53225cf5336394c4f69b (patch)
tree03c6f530f32dbf1d307e1b6a8fe809f2e668b892
parent2f83bdd6822c7c68c32ec6985b309e7f3612cf18 (diff)
downloadperlweeklychallenge-club-17fd72285fae20e3f79d53225cf5336394c4f69b.tar.gz
perlweeklychallenge-club-17fd72285fae20e3f79d53225cf5336394c4f69b.tar.bz2
perlweeklychallenge-club-17fd72285fae20e3f79d53225cf5336394c4f69b.zip
PWC 003 Task #2
-rw-r--r--challenge-003/bob-lied/perl/ch-2.pl48
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-003/bob-lied/perl/ch-2.pl b/challenge-003/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..c30ed80b37
--- /dev/null
+++ b/challenge-003/bob-lied/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-2.pl
+#=============================================================================
+# Copyright (c) 2021, Bob Lied
+#=============================================================================
+# Perl Weekly Challenge Week 3, 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.
+#=============================================================================
+
+use strict;
+use warnings;
+use v5.32;
+
+use experimental qw/ signatures /;
+no warnings "experimental::signatures";
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+my $NumRows = shift;
+die "Usage: $0 N, where N >= 1" unless $NumRows > 0;
+
+sub pascalTriangle($n)
+{
+ say "1";
+ my $prevRow = [ 1, 1 ];
+ say "@$prevRow" if $n >= 2;
+
+ while ( --$n > 1 )
+ {
+ my $nextRow = [ 1 ];
+ for ( my $c = 0 ; $c < scalar(@$prevRow)-1; $c++ )
+ {
+ push @$nextRow, $prevRow->[$c] + $prevRow->[$c+1];
+ }
+ push @$nextRow, 1;
+ say "@$nextRow";
+ $prevRow = $nextRow;
+ }
+
+}
+
+pascalTriangle($NumRows);