| Fred Drake | ca6b4fe | 1998-04-28 18:28:21 +0000 | [diff] [blame] | 1 | % LaTeX produced by Fred L. Drake, Jr. <fdrake@acm.org>, with an | 
|  | 2 | % example based on the PyModules FAQ entry by Aaron Watters | 
|  | 3 | % <arw@pythonpros.com>. | 
|  | 4 |  | 
| Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 5 | \section{\module{bisect} --- | 
|  | 6 | Array bisection algorithms for binary searching.} | 
| Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | \declaremodule{standard}{bisect} | 
|  | 8 |  | 
| Fred Drake | edf6b1f | 1998-07-27 22:16:46 +0000 | [diff] [blame] | 9 | \modulesynopsis{Array bisection algorithms for binary searching.} | 
| Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 10 |  | 
| Fred Drake | ca6b4fe | 1998-04-28 18:28:21 +0000 | [diff] [blame] | 11 |  | 
|  | 12 |  | 
|  | 13 | This module provides support for maintaining a list in sorted order | 
|  | 14 | without having to sort the list after each insertion.  For long lists | 
|  | 15 | of items with expensive comparison operations, this can be an | 
|  | 16 | improvement over the more common approach.  The module is called | 
|  | 17 | \module{bisect} because it uses a basic bisection algorithm to do its | 
|  | 18 | work.  The source code may be used a useful reference for a working | 
|  | 19 | example of the algorithm (i.e., the boundary conditions are already | 
|  | 20 | right!). | 
|  | 21 |  | 
|  | 22 | The following functions are provided: | 
|  | 23 |  | 
|  | 24 | \begin{funcdesc}{bisect}{list, item\optional{, lo\optional{, hi}}} | 
|  | 25 | Locate the proper insertion point for \var{item} in \var{list} to | 
|  | 26 | maintain sorted order.  The parameters \var{lo} and \var{hi} may be | 
|  | 27 | used to specify a subset of the list which should be considered.  The | 
|  | 28 | return value is suitable for use as the first parameter to | 
|  | 29 | \code{\var{list}.insert()}. | 
|  | 30 | \end{funcdesc} | 
|  | 31 |  | 
|  | 32 | \begin{funcdesc}{insort}{list, item\optional{, lo\optional{, hi}}} | 
|  | 33 | Insert \var{item} in \var{list} in sorted order.  This is equivalent | 
|  | 34 | to \code{\var{list}.insert(bisect.bisect(\var{list}, \var{item}, | 
|  | 35 | \var{lo}, \var{hi}), \var{item})}. | 
|  | 36 | \end{funcdesc} | 
|  | 37 |  | 
|  | 38 |  | 
|  | 39 | \subsection{Example} | 
|  | 40 | \nodename{bisect-example} | 
|  | 41 |  | 
|  | 42 | The \function{bisect()} function is generally useful for categorizing | 
|  | 43 | numeric data.  This example uses \function{bisect()} to look up a | 
|  | 44 | letter grade for an exam total (say) based on a set of ordered numeric | 
|  | 45 | breakpoints: 85 and up is an `A', 75..84 is a `B', etc. | 
|  | 46 |  | 
|  | 47 | \begin{verbatim} | 
|  | 48 | >>> grades = "FEDCBA" | 
|  | 49 | >>> breakpoints = [30, 44, 66, 75, 85] | 
|  | 50 | >>> from bisect import bisect | 
|  | 51 | >>> def grade(total): | 
|  | 52 | ...           return grades[bisect(breakpoints, total)] | 
|  | 53 | ... | 
|  | 54 | >>> grade(66) | 
|  | 55 | 'C' | 
|  | 56 | >>> map(grade, [33, 99, 77, 44, 12, 88]) | 
|  | 57 | ['E', 'A', 'B', 'D', 'F', 'A'] | 
|  | 58 | \end{verbatim} |