diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-02-17 12:33:45 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-02-17 12:33:45 +0000 |
| commit | 52286f1da0c12f0b5f4fe59a2cb4b17e9253aa36 (patch) | |
| tree | fc44ecfb0db436b053ca75cc5a5ca8481268d32f | |
| parent | 07c7d869d7c7a930b5055adec2d69f7381e3c440 (diff) | |
| download | perlweeklychallenge-club-52286f1da0c12f0b5f4fe59a2cb4b17e9253aa36.tar.gz perlweeklychallenge-club-52286f1da0c12f0b5f4fe59a2cb4b17e9253aa36.tar.bz2 perlweeklychallenge-club-52286f1da0c12f0b5f4fe59a2cb4b17e9253aa36.zip | |
pushing some notes
| -rw-r--r-- | challenge-152/james-smith/perl/ch-1.pl | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/challenge-152/james-smith/perl/ch-1.pl b/challenge-152/james-smith/perl/ch-1.pl index 5da1f50415..bf931d357f 100644 --- a/challenge-152/james-smith/perl/ch-1.pl +++ b/challenge-152/james-smith/perl/ch-1.pl @@ -23,6 +23,12 @@ is( min_path_anydir_stot( $_->[0] ), $_->[2] ) foreach @TESTS; done_testing(); +## For each entry - we store tuple [ total, "path" ]. +## We create a "row" below the triangle with "0"s and empty paths... +## We work back up the tree, and for each cell in the row we +## add the left or right sub-tree depending on which has the lowest +## value... + sub min_path { 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]}; @@ -30,6 +36,7 @@ sub min_path { $p[0][0]; } +## Without he tuple containing the total/sequence sub min_path_total { my @p = (0) x (1+@{$_[0]}); @p = map { $_ + $p[$p[0]<$p[1]?0:1], (shift @p)x 0 } @{$_} for reverse @{$_[0]}; @@ -48,6 +55,10 @@ sub min_path_anydir { $res; } +## Cheeky "one-liner" version - use sort instead of for loop.... +## +## After we have pushed the value we then update the total {which is the last element of order} + sub min_path_anydir_sort { my($res,@order)= 0; (push @order, [sort {$a<=>$b} @{$_}]->[0]), $res+=$order[-1] for @{$_[0]}; @@ -65,6 +76,8 @@ sub min_path_anydir_total { $res; } +## Simpler version of the sort method above which only does total.. + sub min_path_anydir_stot { my $res=0; $res += [sort {$a<=>$b} @{$_}]->[0] for @{$_[0]}; |
