diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-31 05:28:27 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-31 05:28:27 +0100 |
| commit | 87c6f1e47af81cce9da9dde6e73ca9de5bd77367 (patch) | |
| tree | de9eeee6aadc5c7cad2fe885243a8ddf8196fa60 /challenge-076/james-smith | |
| parent | bd62d3ce8a741517052c68bd21ef8a8db8f9a8f6 (diff) | |
| download | perlweeklychallenge-club-87c6f1e47af81cce9da9dde6e73ca9de5bd77367.tar.gz perlweeklychallenge-club-87c6f1e47af81cce9da9dde6e73ca9de5bd77367.tar.bz2 perlweeklychallenge-club-87c6f1e47af81cce9da9dde6e73ca9de5bd77367.zip | |
- Added template for Challenge 076.
Diffstat (limited to 'challenge-076/james-smith')
| -rw-r--r-- | challenge-076/james-smith/README.md | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-076/james-smith/README.md b/challenge-076/james-smith/README.md new file mode 100644 index 0000000000..54043e40b0 --- /dev/null +++ b/challenge-076/james-smith/README.md @@ -0,0 +1,34 @@ +Solutions by James Smith. + +# Challenge 1 - Coins Sum + +This is just begging for a recursive solution. + +```perl +sub csm { + my $t = shift; + return @{$mem{"$t @_"}||=[map {my $a=$_; $t==$a?[$a]: + map {[$a,@{$_}]} csm($t-$a,grep {$a<=$_&&$_<=$t} @_)} @_] }; +} +``` + +Notes: + + * %mem is a memoisation cache as it helps to not need to re-compute higher totals more than once - speeds up searches for large coin sums... not essential but nice to have + +How it works: + + * Loop through all coin values available + * if the coin is the same as the amount required return a single array containing that value; + * if not remove that from the amount required and call again. For all values returned prepend the coin value to each array in the list + * when calling again remove any coins which are less than the "current coin" and greater than the amount required + +Caveats & assumptions: + + * No input checking is performed - assumes the options passed are all valid and greater than 0; + +# Challenge 2 - Histogram rectangle + +Much simpler this time. + +First we get the size of the box (max value) and render chart then we look for the maximum rectangle size - which is computed as the maximum value of the distance between any two points (inclusive) multiplied by the lowest value in between the two values. (Maxi-min problem) |
