(Codewars Kata) Memoised Log Cutting
I see three problems with this function:


Why calculate the values up to len(p), when you are actually interested in p[n] (which may be larger or smaller than len(p))? In the case of n &gt; len(p) your code actually raises an exception (and it seems not to be excluded by the rules, but also not to be checked by the online judge).
This is not really using memoization, since you calculate everything again whenever the function is called.
You mutate p, which might lead to unexpected results (in general), although here it happens to work out fine.


The second (and third) problem are not so easy to fix (probably only by memoizing all different p ever seen and for each of them the maximum n used). Also the online judge may or may not use the same p again.

The first one however is an easy fix:

def cut_log(p, n):
    for i in range(2, n + 1):
        p[i] = max(p[i-k] + p[k] for k in range((i//2)+1))
    return p[n]