Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09d9f86

Browse files
author
Skip Montanaro
committed
add /F's PriorityQueue example
1 parent ffd3a42 commit 09d9f86

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

Doc/lib/libbisect.tex

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ \section{\module{bisect} ---
6161
\end{funcdesc}
6262

6363

64-
\subsection{Example}
64+
\subsection{Examples}
6565
\nodename{bisect-example}
6666

6767
The \function{bisect()} function is generally useful for categorizing
@@ -81,3 +81,21 @@ \subsection{Example}
8181
>>> map(grade, [33, 99, 77, 44, 12, 88])
8282
['E', 'A', 'B', 'D', 'F', 'A']
8383
\end{verbatim}
84+
85+
The bisect module can be used with the Queue module to implement a priority
86+
queue (example courtesy of Fredrik Lundh): \index{Priority Queue}
87+
88+
\begin{verbatim}
89+
import Queue, bisect
90+
91+
class PriorityQueue(Queue.Queue):
92+
def _put(self, item):
93+
bisect.insort(self.queue, item)
94+
95+
# usage
96+
queue = PriorityQueue(0)
97+
queue.put((2, "second"))
98+
queue.put((1, "first"))
99+
queue.put((3, "third"))
100+
priority, value = queue.get()
101+
\end{verbatim}

0 commit comments

Comments
 (0)