aboutsummaryrefslogtreecommitdiff
path: root/challenge-108/abigail
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-04-12 20:03:28 +0200
committerAbigail <abigail@abigail.be>2021-04-12 22:17:36 +0200
commitfa485e6665431156c33d7c7ad3fa17be1b54c596 (patch)
treefa2f19603398f244714c1c1478dc6582473aa797 /challenge-108/abigail
parentf02c5746861535f4471879d33ff63759bbcd6e9a (diff)
downloadperlweeklychallenge-club-fa485e6665431156c33d7c7ad3fa17be1b54c596.tar.gz
perlweeklychallenge-club-fa485e6665431156c33d7c7ad3fa17be1b54c596.tar.bz2
perlweeklychallenge-club-fa485e6665431156c33d7c7ad3fa17be1b54c596.zip
Perl solution for week 108, part 2
Diffstat (limited to 'challenge-108/abigail')
-rw-r--r--challenge-108/abigail/README.md1
-rw-r--r--challenge-108/abigail/perl/ch-2.pl68
-rw-r--r--challenge-108/abigail/t/ctest.ini21
-rw-r--r--challenge-108/abigail/t/input-2-20
-rw-r--r--challenge-108/abigail/t/input-2-30
-rw-r--r--challenge-108/abigail/t/input-2-40
-rw-r--r--challenge-108/abigail/t/output-2-2.exp1
-rw-r--r--challenge-108/abigail/t/output-2-3.exp1
-rw-r--r--challenge-108/abigail/t/output-2-4.exp1
9 files changed, 93 insertions, 0 deletions
diff --git a/challenge-108/abigail/README.md b/challenge-108/abigail/README.md
index 98a4523d47..bff5cad944 100644
--- a/challenge-108/abigail/README.md
+++ b/challenge-108/abigail/README.md
@@ -28,6 +28,7 @@ more informations.
`{b}{a,c,d}`, `{c}{a,b,d}`, `{d}{a,b,c}`
### Solutions
+* [Perl](perl/ch-2.pl)
### Blog
diff --git a/challenge-108/abigail/perl/ch-2.pl b/challenge-108/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..d89e32391e
--- /dev/null
+++ b/challenge-108/abigail/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/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 [plain|fetch|compute]
+#
+#
+# Three solutions, depending on the command line argument:
+# 1) We just print the first 10 Bell numbers. The simplest solution.
+# 2) We fetch the first 10 Bell numbers from the OEIS.
+# 3) We calculate the Bell numbers from the Bell triangle.
+#
+
+my $COUNT = 10;
+
+my $TYPE_PLAIN = 0;
+my $TYPE_FETCH = 1;
+my $TYPE_COMPUTE = 2;
+
+my $type = $TYPE_PLAIN; # Default
+ $type = $TYPE_FETCH if @ARGV && $ARGV [0] eq "fetch";
+ $type = $TYPE_COMPUTE if @ARGV && $ARGV [0] eq "compute";
+
+
+if ($type == $TYPE_PLAIN) {
+ say "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147";
+}
+elsif ($type == $TYPE_FETCH) {
+ use OEIS;
+ $, = ", ";
+ say oeis (110 => 10)
+}
+elsif ($type == $TYPE_COMPUTE) {
+ my $COUNT = 10;
+
+ #
+ # Create Bell triangle (See https://en.wikipedia.org/wiki/Bell_triangle)
+ # The Bell numbers (starting from the second one), are on the
+ # right diagonal.
+ #
+ # The Bell triangle is defined as follows:
+ # Bell (0, 0) = 1
+ # Bell (n, 0) = Bell (n - 1, n - 1), k > 0
+ # Bell (n, m) = Bell (n, m - 1) + Bell (n - 1, m - 1), n > 0, m > 0
+ #
+ my @bell = [1];
+ for (my $x = 1; $x < $COUNT - 1; $x ++) {
+ $bell [$x] [0] = $bell [$x - 1] [-1];
+ for (my $y = 1; $y <= $x; $y ++) {
+ $bell [$x] [$y] = $bell [$x] [$y - 1] + $bell [$x - 1] [$y - 1]
+ }
+ }
+
+ $, = ", ";
+ say 1, map {$$_ [-1]} @bell;
+}
diff --git a/challenge-108/abigail/t/ctest.ini b/challenge-108/abigail/t/ctest.ini
index c6b6322f48..1a6f8ee862 100644
--- a/challenge-108/abigail/t/ctest.ini
+++ b/challenge-108/abigail/t/ctest.ini
@@ -6,6 +6,27 @@
[names]
1-1 = Memory address
2-1 = Fixed output
+2-2 = Fixed output/plain
+2-3 = Fixed output/fetch
+2-4 = Fixed output/compute
[1-1]
no_input = 1
+
+[2-1/perl]
+skip = "Not for Perl"
+
+[2-2,2-3,2-4]
+skip = "Only for Perl"
+
+[2-2/perl]
+skip = 0
+args = plain
+
+[2-3/perl]
+skip = 0
+args = fetch
+
+[2-4/perl]
+skip = 0
+args = compute
diff --git a/challenge-108/abigail/t/input-2-2 b/challenge-108/abigail/t/input-2-2
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-108/abigail/t/input-2-2
diff --git a/challenge-108/abigail/t/input-2-3 b/challenge-108/abigail/t/input-2-3
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-108/abigail/t/input-2-3
diff --git a/challenge-108/abigail/t/input-2-4 b/challenge-108/abigail/t/input-2-4
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-108/abigail/t/input-2-4
diff --git a/challenge-108/abigail/t/output-2-2.exp b/challenge-108/abigail/t/output-2-2.exp
new file mode 100644
index 0000000000..52ad7f9d63
--- /dev/null
+++ b/challenge-108/abigail/t/output-2-2.exp
@@ -0,0 +1 @@
+1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147
diff --git a/challenge-108/abigail/t/output-2-3.exp b/challenge-108/abigail/t/output-2-3.exp
new file mode 100644
index 0000000000..52ad7f9d63
--- /dev/null
+++ b/challenge-108/abigail/t/output-2-3.exp
@@ -0,0 +1 @@
+1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147
diff --git a/challenge-108/abigail/t/output-2-4.exp b/challenge-108/abigail/t/output-2-4.exp
new file mode 100644
index 0000000000..52ad7f9d63
--- /dev/null
+++ b/challenge-108/abigail/t/output-2-4.exp
@@ -0,0 +1 @@
+1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147