maximum() efficency

Steve R. Hastings steve at hastings.org
Mon Mar 27 01:01:49 EST 2006


On Mon, 27 Mar 2006 00:11:04 +0000, Paul McGuire wrote:
> The doc string is not correct.

The fault is mine, I'm afraid.  I had thought I was copying that from an
intact original version, but I must have edited it.  I don't remember
doing it, but I must have.

To check, I went to Sourceforge and downloaded goopy again.  Here, for
the record, is the original Google maximum():



def maximum(cmp, lst):
  """maximum(cmp, lst)

  Returns the maximal element in non-empty list LST with elements
  compared via CMP() which should return values with the same semantics
  as Python's cmp().  If there are several maximal elements, the last
  one is returned.
  """

  if not lst:
    raise ValueError, 'empty list'

  maxval = lst[0]

  for i in xrange(1, len(lst)):
    v = lst[i]
    if cmp(maxval, v) <= 0:
      maxval = v

  return maxval



I apologize for the error.

P.S. I'm happy to see that Google uses "lst" for a list.  I do, too.  I
don't much like the convention of using "L" for a list, but I agree with
Guido that using "l" is bad (looks too much like a one: "1"), so "lst" it
is.

I would have put the "cmp" argument second, and made it default to the
built-in Python "cmp" function if not specified.  Oh, well.
-- 
Steve R. Hastings    "Vita est"
steve at hastings.org    http://www.blarg.net/~steveha




More information about the Python-list mailing list