This file contains hidden or 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
| import heapq | |
| from typing import Union | |
| class PriorityQueue: | |
| """A priority queue implementation based on heapq""" | |
| insertion_count = 0 | |
| def __init__(self) -> None: |
This file contains hidden or 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
| from collections import deque | |
| from typing import Any | |
| class Stack: | |
| """A stack implementation based on deque""" | |
| def __init__(self) -> None: | |
| """Initialize the stack as an empty deque""" | |
| self.items = deque() |
This file contains hidden or 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
| def is_empty(self) -> bool: | |
| """Returns True is the stack is empty""" | |
| return not self.items |
This file contains hidden or 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
| def push(self, item: Any) -> None: | |
| """Add an item to the top of the stack""" | |
| self.items.append(item) |
This file contains hidden or 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
| def pop(self) -> Any: | |
| """Return and remove the item at the top of the stack""" | |
| if self.is_empty(): | |
| return None | |
| return self.items.pop() |
This file contains hidden or 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
| def peek(self) -> Any: | |
| """Return the item at the top of the stack""" | |
| if self.is_empty(): | |
| return None | |
| return self.items[-1] |
This file contains hidden or 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
| def size(self) -> int: | |
| """Return the number of items in the stack""" | |
| return len(self.items) |
This file contains hidden or 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
| from collections import deque | |
| from typing import Any | |
| class Stack: | |
| """A stack implementation based on deque""" | |
| def __init__(self) -> None: | |
| """Initialize the stack as an empty deque""" | |
| self.items = deque() |
This file contains hidden or 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
| from collections import deque | |
| from typing import Any | |
| class Queue: | |
| """A queue implementation based on deque""" | |
| def __init__(self) -> None: | |
| """Initialize the queue as an empty deque""" | |
| self.items = deque() |
This file contains hidden or 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
| def is_empty(self) -> bool: | |
| """Returns True is the queue is empty""" | |
| return not self.items |