aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2022-11-21 08:02:20 +0000
committerGitHub <noreply@github.com>2022-11-21 08:02:20 +0000
commit690c285cbe6d54e23bac98561cdbe25c070c58a6 (patch)
tree28638423696bc9adaaeed54bd84f1779eae842eb
parent8ea75399a70f8ab625f105a9d5ca011759e9dc19 (diff)
downloadperlweeklychallenge-club-690c285cbe6d54e23bac98561cdbe25c070c58a6.tar.gz
perlweeklychallenge-club-690c285cbe6d54e23bac98561cdbe25c070c58a6.tar.bz2
perlweeklychallenge-club-690c285cbe6d54e23bac98561cdbe25c070c58a6.zip
Update README.md
-rw-r--r--challenge-191/james-smith/README.md68
1 files changed, 27 insertions, 41 deletions
diff --git a/challenge-191/james-smith/README.md b/challenge-191/james-smith/README.md
index 6aa5575439..324a5aa9e5 100644
--- a/challenge-191/james-smith/README.md
+++ b/challenge-191/james-smith/README.md
@@ -114,49 +114,35 @@ These observations lead us to the following code...
my %cache;
sub cute {
- ## (0) Clear cache...
- %cache=();
- ## (1) If n is 1 short cut and return 1
- $_[0]==1 ? 1 : _cute_count( 0,
- ## (2) Just keep the lists
- map { $_->[1] }
- ## (3) Sort so the shortest lists are first - then sort by integer
- sort { @{$a->[1]} <=> @{$b->[1]} ||
- $a->[0] <=> $b->[0]
- }
- ## (4) Find all values between 1 & n which are either a factor or
- ## multiple. Store each as pair, of the number + all values.
- map {[ ($a=$_), [
- grep { !( $_%$a && $a%$_ ) } 1 .. $_[0]
- ] ]}
- ## (5) Looping over 1 to n
- 1 .. $_[0]
+ %cache=(); ## (0) Clear cache...
+ $_[0]==1 ? 1 : _cute( 0, ## (1) If n is 1 short cut and return 1
+ map { $_->[1] } ## (2) Just keep the lists
+ sort { @{$a->[1]} <=> @{$b->[1]} || ## (3) Sort so the shortest lists are
+ $a->[0] <=> $b->[0] ## first - then sort by integer
+ } ## (4) Find all values between 1 & n
+ map {[ ($a=$_), [ ## which are either a factor
+ grep { !( $_%$a && $a%$_ ) } ## or multiple, store each as pair
+ 1 .. $_[0] ## of number and list of values
+ ] ]} ##
+ 1 .. $_[0] ## (5) Looping over 1 to n
)
}
-
-sub _cute_count {
- ## (6) We shift of the index number of seen numbers
- ## and also the next group of possible numbers...
- my( $seen, $next ) = ( shift, shift );
- ## (7) If we have already computed the value return...
- ## (8) otherwise we loop over the values possible in the
- ## "nth" position (this is loose as they aren't ordered directly)
- ## by " but by the count {we are only counting so don't need to
- ## produce numbers}
- $cache{$seen} //= sum0 map {
- ## (9) We sum up the value for each value in this list which hasn't
- ## been seen (and return it!)
- ($seen & 1<<$_) ? 0
- ## (10) If there is only 1 number left in the list we count 1
- ## (as all numbers can be in the last position)
- : @_ < 2 ? 1
- ## (11) o/w we call this method again after knocking out the number
- : _cute_count( $seen | 1<<$_ , @_ )
- } @{$next}
-}
- ## Note we don't use a string as a key - but use a bit mast -
- ## #9 & #11 using "|" to set a bit & "&" to check it has
- ## been set.
+sub _cute {
+ my( $seen, $next ) = ( shift, shift ); ## (6) We shift of the index number of seen numbers
+ ## and also the next group of possible numbers...
+ $cache{$seen} //= sum0 map { ## (7) If we have already computed the value return...
+ ## (8) otherwise we loop over the values possible in the
+ ## "nth" position (this is loose as they aren't
+ ## ordered directly) by " but by the count {we are
+ ## only counting so don't need to produce numbers}
+ ## (9) We sum up the value for each value in this list
+ ($seen & 1<<$_) ? 0 ## which hasn't been seen (and return it!)
+ : @_ < 2 ? 1 ## (10) If there is only 1 number left in the list we
+ ## count 1 (as all numbers can be in the last position)
+ : _cute( $seen | 1<<$_ , @_ ) ## (11) o/w we call this method again after tagging number seen
+ } @{$next} ## Note we don't use a string as a key - but use a bit mast -
+} ## #9 & #11 using "|" to set a bit & "&" to check
+ ## it has been set.
```
or without comments: