aboutsummaryrefslogtreecommitdiff
path: root/challenge-077/james-smith
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-09-07 07:23:58 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-09-07 07:23:58 +0100
commit94882719ec2d2ea35cfaf241de7d45b1b2ed1162 (patch)
tree3e0ea5aa4eba5d4981d37576f04db6d323088baa /challenge-077/james-smith
parent23c28e01b00c58791375e66d109355af541592ae (diff)
downloadperlweeklychallenge-club-94882719ec2d2ea35cfaf241de7d45b1b2ed1162.tar.gz
perlweeklychallenge-club-94882719ec2d2ea35cfaf241de7d45b1b2ed1162.tar.bz2
perlweeklychallenge-club-94882719ec2d2ea35cfaf241de7d45b1b2ed1162.zip
- Added template for Challenge #077.
Diffstat (limited to 'challenge-077/james-smith')
-rw-r--r--challenge-077/james-smith/README.md34
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-077/james-smith/README.md b/challenge-077/james-smith/README.md
new file mode 100644
index 0000000000..54043e40b0
--- /dev/null
+++ b/challenge-077/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)