aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-28 01:25:33 +0100
committerAbigail <abigail@abigail.be>2021-01-28 01:25:33 +0100
commit58450e134351ade0f649cc27befe6931741c2c56 (patch)
tree65aa848bd0b993486c0483cb01b8c591a85850cc
parentf0cb8f81cd3d3512c48286cd4a62add74ccbaf51 (diff)
downloadperlweeklychallenge-club-58450e134351ade0f649cc27befe6931741c2c56.tar.gz
perlweeklychallenge-club-58450e134351ade0f649cc27befe6931741c2c56.tar.bz2
perlweeklychallenge-club-58450e134351ade0f649cc27befe6931741c2c56.zip
Perl solution for week 3, part 2
-rw-r--r--challenge-003/abigail/README.md1
-rw-r--r--challenge-003/abigail/perl/ch-2.pl44
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md
index 9801393564..818ae3c474 100644
--- a/challenge-003/abigail/README.md
+++ b/challenge-003/abigail/README.md
@@ -37,3 +37,4 @@ rows from the command line. The Pascal Triangle should have at least
[wikipedia](https://en.wikipedia.org/wiki/Pascal%27s_triangle) page.
### Solutions
+* [Perl](perl/ch-2.pl)
diff --git a/challenge-003/abigail/perl/ch-2.pl b/challenge-003/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..a6af9168d0
--- /dev/null
+++ b/challenge-003/abigail/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# See ../README.md
+#
+
+#
+# Run as: perl ch-2.pl < input-file
+#
+
+chomp (my $rows = <>);
+
+#
+# 0-th row
+#
+my @row = (1);
+say "@row";
+
+foreach (1 .. $rows) {
+ #
+ # Calculate the next row from the current row
+ #
+ my @new = map {($_ == 0 ? 0 : $row [$_ - 1]) +
+ ($_ == @row ? 0 : $row [$_])} 0 .. @row;
+
+ #
+ # Print
+ #
+ say "@new";
+
+ #
+ # New row becomes current row
+ #
+ @row = @new;
+}