-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4596ab
commit dd3b2cf
Showing
17 changed files
with
270 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Queue | ||
|
||
## collections.dequeue | ||
* queue: only supports removing elements from the back | ||
* dequeue: also supports removing elements form the front | ||
|
||
import collections | ||
|
||
q = collections.deque([1, 2, 3], maxlen=None) | ||
q.append(4) | ||
q.extend([5, 6]) | ||
while q: | ||
print(q.popleft()) | ||
|
||
|
||
## queue.Queue | ||
* Used for synchronizing threads | ||
* thread-safe | ||
* Use collections.dequeue otherwise | ||
|
||
import queue | ||
|
||
q = queue.Queue(maxsize=0) | ||
q.put(1) | ||
q.put(2) | ||
while not q.empty(): | ||
print(q.get()) | ||
|
||
|
||
# Priority queue | ||
|
||
## heapq | ||
* see heapq.txt | ||
|
||
q = [] | ||
heapq.heappush(q, 3) | ||
heapq.heappush(q, 1) | ||
heapq.heappush(q, 2) | ||
|
||
q = [3, 1, 2] | ||
heapq.heapify(q) // in-place | ||
|
||
while q: | ||
print(heapq.heappop(q)) | ||
|
||
## queue.PriorityQueue | ||
* For threads (like queue.Queue) | ||
* Wrapper around heapq | ||
|
||
pq = queue.PriorityQueue() | ||
|
||
pq.put((1, "Task 1")) | ||
pq.put((3, "Task 3")) | ||
pq.put((2, "Task 2")) | ||
|
||
print(pq.get()) # Output: (1, 'Task 1') | ||
print(pq.get()) # Output: (2, 'Task 2') | ||
print(pq.get()) # Output: (3, 'Task 3') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.