Need help in understanding a python code
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun Nov 16 05:31:16 EST 2008
On Sun, 16 Nov 2008 01:50:16 -0800, John Machin wrote:
> > def A(w, v, i,j):
> > if i == 0 or j == 0: return 0
> > if w[i-1] > j: return A(w, v, i-1, j)
> > if w[i-1] <= j: return max(A(w,v, i-1, j), v[i-1] +
> > A(w,v, i-1, j - w[i-1]))
>> I am reading this blog
>>
>> http://20bits.com/articles/introduction-to-dynamic-programming/
>
> I suggest that you don't bother reading a blog written by somebody who
> (presumably consciously) keyed in that "if w[i-1] <= j: " above.
That is a translation of standard terminology for a hybrid function.
Mathematics doesn't have an "else", so you write hybrid functions by
enumerating each branch as an if.
While it's not especially good Python technique, it's a perfectly
idiomatic mathematical expression, and shouldn't be the basis for
dismissing an entire blog.
--
Steven
More information about the Python-list
mailing list