Need help in understanding a python code

John Machin sjmachin at lexicon.net
Sun Nov 16 13:34:40 CET 2008


On Nov 16, 11:04 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sun, 16 Nov 2008 02:41:03 -0800, John Machin wrote:
> > 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]))
>
> Which is also not valid terminology for hybrid functions.

I couldn't care less. It's valid and efficient (compared to the
original) Python.

> >> 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.
>
> And he's written Python code. Perfectly valid Python code. Just because
> it is not what you consider to be idiomatic Python code isn't a good
> reason to dismiss his entire blog.
>
> What you've done is rather like me saying that because you failed to use
> a colon after "required", and therefore haven't written what *I* consider
> good English style, not only is your specific post best avoided, but
> *all* your posts should be avoided. I trust you understand the logical
> fallacy I would be making, which you have already made.

Nothing to do with style. It was the screaming inefficiency of:
   if non_trivial_condition: return x
   if not non_trivial_condition: return y
that fired me up.

> http://en.wikipedia.org/wiki/Style_over_substance_fallacy

Quoted Wikipedia -> instant disqualification -> you lose. Good night.




More information about the Python-list mailing list