diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-02-13 09:59:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-13 09:59:09 +0000 |
| commit | 0fc74df4cb758def1acbae228e15c66d0cc28232 (patch) | |
| tree | 0adae21a45dab65a8902dd2fd69f3ea9405891d6 | |
| parent | c5004cd0b222eca75ed9ffbd8facf4959cedf483 (diff) | |
| parent | d5da61dc5c795752917d1d74d57be5613fb0f63b (diff) | |
| download | perlweeklychallenge-club-0fc74df4cb758def1acbae228e15c66d0cc28232.tar.gz perlweeklychallenge-club-0fc74df4cb758def1acbae228e15c66d0cc28232.tar.bz2 perlweeklychallenge-club-0fc74df4cb758def1acbae228e15c66d0cc28232.zip | |
Merge pull request #5644 from drbaggy/master
Simplified code for 2nd challenge.
| -rw-r--r-- | challenge-151/james-smith/README.md | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/challenge-151/james-smith/README.md b/challenge-151/james-smith/README.md index b6806a1ee6..01e099b2ca 100644 --- a/challenge-151/james-smith/README.md +++ b/challenge-151/james-smith/README.md @@ -62,6 +62,8 @@ We will only ever skip one or two houses at each "jump", we will never skip more `--->------>------>------>------>------>---' ``` +### First attempt ... + Here we construct and array of the best total we could achieve if we stopped at the 1st, 2nd, 3rd houses etc... For the first two: @@ -82,3 +84,17 @@ sub rob { ``` *We could have written the second line with `1` & `0` not `-1` and `-2` but there is something about the symmetry of the two lines which is poetic* + +### Second attempt ... + +As we only need the previous 2 houses, we can do away with the array and just work with the score of the two previous entries. + +```perl +sub rob_no_array { + my$p=my$q=0; + ($p,$q)=($q,$q>$p+$_?$q:$p+$_)for@_; + $q; +} +``` + +Speedwise this is about 10% faster... |
