Need help in understanding a python code

Aaron Brady castironpi at gmail.com
Sun Nov 16 00:44:28 EST 2008


See below.

On Nov 15, 11:15 pm, "Meryl Silverburgh" <silverburgh.me... at gmail.com>
wrote:
> This is the full source code:
> 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/
>
> On Sat, Nov 15, 2008 at 10:54 PM, Chris Rebert <c... at rebertia.com> wrote:
> > On Sat, Nov 15, 2008 at 8:41 PM, silverburgh.me... at gmail.com
> > <silverburgh.me... at gmail.com> wrote:
> >> Hi,
>
> >> I am trying to understand the following line:
> >> # a is an integer array
>
> >> max([(sum(a[j:i]), (j,i))
>
> > This code isn't valid. You have a [ with no closing ].
>
> > Cheers,
> > Chris
> > --
> > Follow the path of the Iguana...
> >http://rebertia.com
>
> >> Can you please tell me what that means,
> >> I think sum(a[j:i] means find the some from a[j] to a[i]
> >> But what is the meaning of the part (j,i)?
>
> >> --
> >>http://mail.python.org/mailman/listinfo/python-list
>
>


>     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]))

This means:

Calculate 'A(w,v, i-1, j)', calculate 'v[i-1] + A(w,v, i-1, j - w
[i-1])', and return whichever is larger.



More information about the Python-list mailing list