aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-07-19 13:09:15 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-07-19 13:09:15 +0100
commit580f84362298c4c14b3b41aa124dd3bb99d448f6 (patch)
tree0848505bf420116e79d2ce1f34d46aa75e48fbb4
parent4015e8568518715d5108979cc183a7f111669176 (diff)
downloadperlweeklychallenge-club-580f84362298c4c14b3b41aa124dd3bb99d448f6.tar.gz
perlweeklychallenge-club-580f84362298c4c14b3b41aa124dd3bb99d448f6.tar.bz2
perlweeklychallenge-club-580f84362298c4c14b3b41aa124dd3bb99d448f6.zip
2nd pass with comments
-rw-r--r--challenge-122/james-smith/perl/ch-1.pl21
-rw-r--r--challenge-122/james-smith/perl/ch-2.pl38
2 files changed, 38 insertions, 21 deletions
diff --git a/challenge-122/james-smith/perl/ch-1.pl b/challenge-122/james-smith/perl/ch-1.pl
index c1d25b5736..e85a90e949 100644
--- a/challenge-122/james-smith/perl/ch-1.pl
+++ b/challenge-122/james-smith/perl/ch-1.pl
@@ -8,13 +8,24 @@ use Test::More;
use Benchmark qw(cmpthese timethis);
use Data::Dumper qw(Dumper);
-my @stream = map {$_*10} 1..50;
+stream( map {$_*10} 1..50 ); ## Push values into stream...
+eval {say stream_average();} until $@; ## Use eval/$@ to repeat until stream dies.
-say stream_average($_) foreach @stream;
+sub stream {
+ state(@stream);
+ @_ ? (push @stream,@_) ## Parameters passed - push to stream
+ : @stream ? shift @stream ## We have entry in stream return it
+ : die; ## exhausted stream die....
+}
sub stream_average {
+ ## Use state variables for the total & count;
state($n,$t);
- $n++; $t+=shift;
- return $t/$n;
-}
+ ## Take next element and add to total
+ ## Increment the count, and return the ratio of the true values
+ ## Note we need to do pre-increment rather than
+ ## post increment so the incremement is done before use.
+
+ return ($t+=stream)/++$n;
+}
diff --git a/challenge-122/james-smith/perl/ch-2.pl b/challenge-122/james-smith/perl/ch-2.pl
index a89fd8e826..9fa94f6fb8 100644
--- a/challenge-122/james-smith/perl/ch-2.pl
+++ b/challenge-122/james-smith/perl/ch-2.pl
@@ -10,23 +10,29 @@ use Data::Dumper qw(Dumper);
## Pre-populate cache with first 3 values...
my %cache = (
- 1 => [1],
- 2 => ['1 1',2],
- 3 => ['1 1 1','1 2','2 1',3],
+ 1 => [qw(1)],
+ 2 => [qw(11 2)],
+ 3 => [qw(111 12 21 3)],
);
-say join "\n",@{pts(4)};
-say "";
-say join "\n",@{pts(5)};
-
+say join "\n",'',"SIZE $_",'',@{pts($_)} foreach 0..20;
+say '';
sub pts {
- my $n = shift;
- return [] if $n < 1;
- return $cache{$n} if exists $cache{$n};
- my @res = (
- map( {"1 $_"} @{pts($n-1)} ),
- map( {"2 $_"} @{pts($n-2)} ),
- map( {"3 $_"} @{pts($n-3)} )
- );
- return $cache{$n} = \@res;
+ ## Let's look at the first points scored - it is either
+ ## 1, 2 or 3.
+ ## So we then look to see how remaining points are scored
+ ## these are 1 - followed by all combinations for n-1
+ ## 2 - followed by all combinations for n-2
+ ## 3 - followed by all combinations for n-3
+ ## The special cases are therefore where any of these values
+ ## are less than 1 - so we need to have pre-populated values
+ ## for 1, 2 and 3 points (1; 11+2; 111+12+21+3 respectively)
+ ## We cache values as we know that we will see certain
+ ## values occuring repeatedly {e.g. the start sequences
+ ## 111, 12, 21 3 all then get all sequences for n-3
+ return $cache{$_[0]} ||= $_[0] < 1 ? [] : [
+ map( {'1'.$_} @{pts( $_[0]-1 )} ),
+ map( {'2'.$_} @{pts( $_[0]-2 )} ),
+ map( {'3'.$_} @{pts( $_[0]-3 )} )
+ ];
}