[Python-checkins] python/nondist/sandbox/statistics statistics.py, 1.4, 1.5

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Tue Feb 17 07:17:44 EST 2004


Update of /cvsroot/python/python/nondist/sandbox/statistics
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6746

Modified Files:
	statistics.py 
Log Message:
Simplify the select() function.



Index: statistics.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/statistics/statistics.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** statistics.py	15 Feb 2004 08:25:20 -0000	1.4
--- statistics.py	17 Feb 2004 12:17:41 -0000	1.5
***************
*** 86,126 ****
          return (s / k) ** 0.5           # population standard deviation
  
- def _partition(iterable):
-     """ Private function supporting select()
- 
-     Chooses the first element as pivot and returns tuple with a
-     list of values less than the pivot, the pivot itself, and a
-     a list of values not less than the pivot.
-     """
- 
-     under, notunder = [], []
-     ua, nua = under.append, notunder.append
-     it = iter(iterable)
-     pivot = it.next()                   # must have at least one element
-     for elem in it:
-         if elem < pivot:
-             ua(elem)
-         else:
-             nua(elem)
-     return under, pivot, notunder
- 
  def select(data, n):
!     """Find the nth rank ordered element (the least value has rank 0)."""
      try:
!         size = len(data)
!     except TypeError:
!         data = tuple(data)
!         size = len(data)
!     if size <= n:
          raise ValueError('not enough elements for the given rank')
-     while True:
-         p, pivot, q = _partition(data)
-         if n < len(p):
-             data = p
-         elif n == len(p):
-             return pivot
-         else:
-             data = q
-             n -= len(p) + 1
  
  def median(data):
--- 86,112 ----
          return (s / k) ** 0.5           # population standard deviation
  
  def select(data, n):
!     """Find the nth rank ordered element (the least value has rank 0).
!     """
      try:
!         while True:
!             under, notunder = [], []
!             ua, nua = under.append, notunder.append
!             it = iter(data)
!             pivot = it.next()
!             for elem in it:
!                 if elem < pivot:
!                     ua(elem)
!                 else:
!                     nua(elem)
!             if n < len(under):
!                 data = under
!             elif n == len(under):
!                 return pivot
!             else:
!                 data = notunder
!                 n -= len(under) + 1
!     except StopIteration:
          raise ValueError('not enough elements for the given rank')
  
  def median(data):




More information about the Python-checkins mailing list