From 17fd72285fae20e3f79d53225cf5336394c4f69b Mon Sep 17 00:00:00 2001 From: boblied Date: Sun, 14 Nov 2021 19:27:38 -0600 Subject: PWC 003 Task #2 --- challenge-003/bob-lied/perl/ch-2.pl | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 challenge-003/bob-lied/perl/ch-2.pl 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); -- cgit