diff options
| author | James Smith <js5@sanger.ac.uk> | 2022-02-17 14:45:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-17 14:45:23 +0000 |
| commit | 978a125c86a7391ba79621e8ae981f8a87645133 (patch) | |
| tree | f87cd3ded748eccfe553b7825cc290102ae74677 | |
| parent | 52286f1da0c12f0b5f4fe59a2cb4b17e9253aa36 (diff) | |
| download | perlweeklychallenge-club-978a125c86a7391ba79621e8ae981f8a87645133.tar.gz perlweeklychallenge-club-978a125c86a7391ba79621e8ae981f8a87645133.tar.bz2 perlweeklychallenge-club-978a125c86a7391ba79621e8ae981f8a87645133.zip | |
Update README.md
| -rw-r--r-- | challenge-152/james-smith/README.md | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/challenge-152/james-smith/README.md b/challenge-152/james-smith/README.md index 8c7c813547..9e00afbeab 100644 --- a/challenge-152/james-smith/README.md +++ b/challenge-152/james-smith/README.md @@ -81,6 +81,25 @@ sub min_path_anydir_total { $res; } ``` +### Solution b - version 2... a 1-liner. + +To reduce the number of lines we can replace the minimum with a sort and take lowest value using `[sort {$a<=>$b} @{$_}]->[0]` this gives the following two functions... + +```perl +sub min_path_anydir_sort { + my($res,@order)= 0; + (push @order, [sort {$a<=>$b} @{$_}]->[0]), $res+=$order[-1] for @{$_[0]}; + say sprintf 'Minimum value %d: [ %s ]', $res, join ', ', @order; + $res; +} + +sub min_path_anydir_stot { + my $res=0; + $res += [sort {$a<=>$b} @{$_}]->[0] for @{$_[0]}; + $res; +} + +``` # Challange 2 - Rectangle Area ***You are given coordinates bottom-left and top-right corner of two rectangles in a 2D plane. Write a script to find the total area covered by the two rectangles.*** |
