aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2022-02-15 17:25:31 +0000
committerGitHub <noreply@github.com>2022-02-15 17:25:31 +0000
commit4caf805c5014c401dfb0d7bfdac0dfccea066d75 (patch)
treeb2cec137e25bb62c467f2a1d2c6d897c2aee36da
parentd6766dc194b9c13a6bdaf34b51dfb29472be9eb7 (diff)
downloadperlweeklychallenge-club-4caf805c5014c401dfb0d7bfdac0dfccea066d75.tar.gz
perlweeklychallenge-club-4caf805c5014c401dfb0d7bfdac0dfccea066d75.tar.bz2
perlweeklychallenge-club-4caf805c5014c401dfb0d7bfdac0dfccea066d75.zip
Update README.md
-rw-r--r--challenge-152/james-smith/README.md14
1 files changed, 6 insertions, 8 deletions
diff --git a/challenge-152/james-smith/README.md b/challenge-152/james-smith/README.md
index 09f8ad0cb4..9aab95bd67 100644
--- a/challenge-152/james-smith/README.md
+++ b/challenge-152/james-smith/README.md
@@ -32,24 +32,22 @@ Each time through the loop we generate a new version of `@p` with the best route
```perl
sub min_path {
- my @p = map { [0,[]] } 0,my @t = reverse @{$_[0]};
- @p = map { $p[0][0] < $p[1][0]
- ? [ $_+$p[0][0], [ $_, @{$p[0][1]} ] ]
- : [ $_+$p[1][0], [ $_, @{$p[1][1]} ] ]
- , (shift @p) x 0
- } @{$_} for @t;
+ my @p = ( [0,[]] ) x (1 + @{$_[0]});
+ @p = map { $p[0][0] < $p[1][0] ? [ $_+$p[0][0], [ $_, @{$p[0][1]} ] ] : [ $_+$p[1][0], [ $_, @{$p[1][1]} ] ], (shift @p) x 0 } @{$_} for reverse @{$_[0]};
say sprintf 'Minimum value %d: [ %s ]', $p[0][0], join ', ', @{$p[0][1]};
$p[0][0];
}
+
```
We can simplify this if we are not worried by the order - by storing a simple value (the minimum total for the path) rather than the pair total/path.
```perl
sub min_path_total {
- my @p = map { 0 } 0, my @t = reverse @{$_[0]};
- @p = map { $_ + $p[ $p[0] < $p[1] ? 0 : 1 ], (shift @p)x 0 } @{$_} for @t;
+ my @p = (0) x (1+@{$_[0]});
+ @p = map { $_ + $p[$p[0]<$p[1]?0:1], (shift @p)x 0 } @{$_} for reverse @{$_[0]};
$p[0];
+
}
```