Need help in understanding a python code
John Machin
sjmachin at lexicon.net
Sun Nov 16 05:41:03 EST 2008
On Nov 16, 9:31 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> 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.
An else is not required.
if w[i-1] > j:
return A(w, v, i-1, j)
return max(A(w,v, i-1, j), v[i-1] + A(w,v, i-1, j - w[i-1]))
> 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.
He's meant to be writing Python code, not mathematical expressions.
More information about the Python-list
mailing list