From 632bbcb73eb161176ef331234890d36ea1571e96 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Fri, 16 Apr 2021 09:54:24 +0100 Subject: added solutions --- challenge-108/james-smith/perl/ch-1.pl | 24 ++++++++++++++++++++++++ challenge-108/james-smith/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 challenge-108/james-smith/perl/ch-1.pl create mode 100644 challenge-108/james-smith/perl/ch-2.pl diff --git a/challenge-108/james-smith/perl/ch-1.pl b/challenge-108/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..8aa29eb291 --- /dev/null +++ b/challenge-108/james-smith/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +my $scalar = 1; +my @array = (1); +my %hash = (1=>1); +my $arrayref = \@array; +my $hashref = \%hash; +my $scalarref = \$scalar; + +say "Scalar ", get_loc( \$scalar ); +say "Scalarref ", get_loc( \$scalarref ); +say "Array ", get_loc( \@array ); +say "Arrayref ", get_loc( \$arrayref ); + +sub get_loc { + return $_[0] =~ s{(?:.*\(|\))}{}rg; +} + diff --git a/challenge-108/james-smith/perl/ch-2.pl b/challenge-108/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..524cde283d --- /dev/null +++ b/challenge-108/james-smith/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +my $ANSWER_10 = '1 1 2 5 15 52 203 877 4140 21147'; +my $ANSWER_20 = '1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322 1382958545 10480142147 82864869804 682076806159 5832742205057'; + +is( "@{ bell_numbers(10) }", $ANSWER_10 ); +is( "@{ bell_numbers(20) }", $ANSWER_20 ); + +done_testing(); + +sub bell_numbers { + my $N = shift; + my @res = my @a = 1; + while(1) { + push @res, my @b = $a[-1]; + return \@res if @res >= $N; + push @b, $b[-1] + $_ foreach @a; + @a = @b; + } +} + + -- cgit From aa1430d0a2213c98716cc17dc96e2b17db1b767d Mon Sep 17 00:00:00 2001 From: drbaggy Date: Fri, 16 Apr 2021 10:04:54 +0100 Subject: added some docs --- challenge-108/james-smith/perl/ch-2.pl | 43 ++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/challenge-108/james-smith/perl/ch-2.pl b/challenge-108/james-smith/perl/ch-2.pl index 524cde283d..9e6d8006f9 100644 --- a/challenge-108/james-smith/perl/ch-2.pl +++ b/challenge-108/james-smith/perl/ch-2.pl @@ -14,14 +14,47 @@ is( "@{ bell_numbers(20) }", $ANSWER_20 ); done_testing(); +## Bell numbers can be computed by using a bell triangle. +## The bell numbers are the last cell of each row of the table. +## +## To get one row from the next. +## * We copy the previous "bell number" the last cell in +## the previous row. +## * Each other element of the row is computed by adding the number +## to the left of it in this row and the element about (and to the +## left of it) +## +## The solution... +## +## * We will use 2 arrays to represent the previous row and +## the current row of the table. @a & @b; +## We initialise the first row to just contain the number 1 #1 +## +## * And at each iteration we remember this last element. #2 +## +## * When we have enough elements in our results array we #3 +## return. We don't need to do last and then return outside +## the loop as we can just return from the subroutine from +## inside the loop! +## +## * In each iteration we initialise the row to contain the +## previous last value... #1 +## and then loop through all the elements in the previous #4 +## array accumulating the total - the ability to get the +## value of the last element of the array with [-1] is +## very useful perl feature here... +## +## * Finally we copy the new row into the previous row and #5 +## continue around the loop.... + sub bell_numbers { my $N = shift; - my @res = my @a = 1; + my @res = my @a = 1; #1 while(1) { - push @res, my @b = $a[-1]; - return \@res if @res >= $N; - push @b, $b[-1] + $_ foreach @a; - @a = @b; + push @res, my @b = $a[-1]; #2 + return \@res if @res >= $N; #3 + push @b, $b[-1] + $_ foreach @a; #4 + @a = @b; #5 } } -- cgit