(I am not sure of the official CS name for the problem in question, so I just gave it the name of the kata).
CLEAR CUTTER'S NEEDS YOUR HELP!
The logging company Clear Cutter's makes its money by optimizing the price-to-length of each log they cut before selling them. An example of one of their price tables is included:
# So a price table p p = [ 0, 1, 5, 8, 9, 10] # Can be imagined as: # length i | 0 1 2 3 4 5 *in feet* # price pi | 0 1 5 8 9 10 *in money*
They hired an intern last summer to create a recursive function for them to easily calculate the most profitable price for a log of length n using price table p as follows:
def cut_log(p, n): if (n == 0): return 0 q = -1 for i in range(1, n+1) q = max(q, p[i] + cut_log(p, n-i)) return q
An example of the working code:
# 5ft = $10, BUT 2ft + 3ft = 5ft -> $5 + $8 = $13 which is greater in value
However, their senior software engineer realized that the number of recursive calls in the function gives it an awful running time of 2^n (as this function iterates through ALL 2^n-1 possibilities for a log of length n).
Having discovered this problem just as you've arrived for your internship, he responsibly delegates the issue to you.
Using the power of Stack Overflow and Google, he wants you to create a solution that runs in Θ(n^2) time so that it doesn't take 5 hours to calculate the optimal price for a log of size 50. (He also suggests to research the problem using the keywords in the tags)
(Be aware that if your algorithm is not efficient, it will attempt to look at 2^49 = 562949953421312 nodes instead of 49^2 = 2401... The solution will automatically fail if it takes longer than 6 seconds... which occurs at right around Log 23)
def cut_log(p, n): for i in range(2, len(p)): p[i] = max(p[i-k] + p[k] for k in range((i//2)+1)) return p[n]
It passes all the tests. I'm putting it up for review because I'm pretty proud of it and experience as shown me that there's usually some improvement to be made.