Skip to content

Instantly share code, notes, and snippets.

View wanderindev's full-sized avatar

Javier Feliu wanderindev

View GitHub Profile
import heapq
from typing import Union
class PriorityQueue:
"""A priority queue implementation based on heapq"""
insertion_count = 0
def __init__(self) -> None:
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()
def is_empty(self) -> bool:
"""Returns True is the stack is empty"""
return not self.items
def push(self, item: Any) -> None:
"""Add an item to the top of the stack"""
self.items.append(item)
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()
def peek(self) -> Any:
"""Return the item at the top of the stack"""
if self.is_empty():
return None
return self.items[-1]
def size(self) -> int:
"""Return the number of items in the stack"""
return len(self.items)
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()
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()
def is_empty(self) -> bool:
"""Returns True is the queue is empty"""
return not self.items