From cd5271b1e65d4e56f1988f5832cebdea90778da0 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 14 Apr 2019 14:47:48 -0500 Subject: Add solution for week 3, problem 2 in Perl 5 --- challenge-003/joelle-maslak/perl5/ch-2.pl | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 challenge-003/joelle-maslak/perl5/ch-2.pl diff --git a/challenge-003/joelle-maslak/perl5/ch-2.pl b/challenge-003/joelle-maslak/perl5/ch-2.pl new file mode 100755 index 0000000000..bdc3cd8c51 --- /dev/null +++ b/challenge-003/joelle-maslak/perl5/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use v5.26; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +if ( !@ARGV ) { die("Provide number of rows") } +if ( $ARGV[0] < 3 ) { die("Must provide at least 3 rows") } +if ( int( $ARGV[0] ) != $ARGV[0] ) { die("Must provide an integer") } + +my $rowcount = $ARGV[0]; + +my @rows; + +for ( my $i = 0; $i < $rowcount; $i++ ) { + my @row; + if ( !$i ) { + @row = 1; + } else { + push @row, 1; + for ( my $j = 1; $j < $i; $j++ ) { + push @row, $rows[ $i - 1 ]->[ $j - 1 ] + $rows[ $i - 1 ]->[$j]; + } + push @row, 1; + } + push @rows, \@row; + + for ( my $j = 1; $j < $rowcount - $i; $j++ ) { print(" ") } + say join( " ", map { sprintf( "%3d", $_ ) } @{ $rows[$i] } ); +} + -- cgit