A simple and intuitive implementation of data structures and algorithms
Project description
SimpleDSA
A simple and intuitive implementation of data structures and algorithms in Python.
Installation
pip install simpledsa
Usage
Priority Queue
from simpledsa import PriorityQueue
# Create a min-heap priority queue (smaller values have higher priority)
pq = PriorityQueue()
# Create a max-heap priority queue (larger values have higher priority)
max_pq = PriorityQueue(lambda x: -x)
# Using items as their own priority
pq.push(3)
pq.push(1)
pq.push(4)
print(pq.pop()) # Output: 1 (smallest value has highest priority)
print(pq.pop()) # Output: 3
print(pq.pop()) # Output: 4
# Using explicit priorities
task_queue = PriorityQueue()
task_queue.push("Write docs", priority=2)
task_queue.push("Fix bug", priority=1)
task_queue.push("Add feature", priority=3)
print(task_queue.pop()) # Output: "Fix bug" (priority 1)
print(task_queue.pop()) # Output: "Write docs" (priority 2)
print(task_queue.pop()) # Output: "Add feature" (priority 3)
# Check if queue is empty
if not pq:
print("Queue is empty")
# Get length of queue
print(len(pq)) # Output: 0
# Peek at highest priority item without removing it
task_queue.push("Important task", priority=1)
print(task_queue.peek()) # Output: "Important task"
Advanced Usage Examples
Context Manager (with statement)
from simpledsa import PriorityQueue
# Queue is automatically cleared when exiting the with block
with PriorityQueue() as pq:
pq.push("task1", 1)
pq.push("task2", 2)
process_tasks(pq) # process tasks here
# queue is now empty
# Great for temporary task processing
with PriorityQueue() as pq:
pq.extend(tasks)
while pq:
process(pq.pop())
Batch Operations
# Add multiple items at once
pq = PriorityQueue()
pq.extend([1, 2, 3]) # items as their own priorities
pq.extend([("task1", 1), ("task2", 2)]) # For (item, priority) pairs
# Create queue from items
pq = PriorityQueue.from_items([1, 2, 3])
# Create queue from (item, priority) pairs
pq = PriorityQueue.from_items_with_priority([("task1", 1), ("task2", 2)])
Iteration
# Non-destructive iteration (keeps items in queue)
pq = PriorityQueue.from_items([3, 1, 4, 1, 5])
for item in pq:
print(item) # prints in priority order: 1, 1, 3, 4, 5
# Destructive iteration (removes items)
for item in pq.pop_all():
process(item) # process items in priority order
Queue Merging
# Merge multiple queues
pq1 = PriorityQueue.from_items([1, 3, 5])
pq2 = PriorityQueue.from_items([2, 4, 6])
merged = PriorityQueue.merge([pq1, pq2])
Priority Functions
from simpledsa import PriorityQueue, priority_functions
# Max heap (larger values have higher priority)
max_pq = PriorityQueue(priority_functions.reverse)
max_pq.extend([1, 2, 3])
print(max_pq.pop()) # 3
# Priority by length
length_pq = PriorityQueue(priority_functions.by_length)
length_pq.extend(["a", "ccc", "bb"])
print(length_pq.pop()) # "a" (shortest)
# Priority by attribute
class Task:
def __init__(self, name, priority):
self.name = name
self.priority = priority
task_pq = PriorityQueue(priority_functions.by_attr('priority'))
tasks = [Task("A", 2), Task("B", 1), Task("C", 3)]
task_pq.extend(tasks)
Features
- Supports both min-heap (default) and max-heap behavior
- Items can serve as their own priority or have explicit priorities
- Stable sorting: items with equal priorities maintain their insertion order
- Standard Python container operations:
len(), boolean evaluation - O(log n) push and pop operations
- O(1) peek and is_empty operations
License
This project is licensed under the MIT License - see the LICENSE file for details.
Development
Setting up development environment
# Clone the repository
git clone https://github.com/dsalathe/simpledsa.git
cd simpledsa
# Install Poetry
curl -sSL https://install.python-poetry.org | python3 -
# Install dependencies
poetry install
# Run tests
poetry run pytest
# Format code
poetry run black .
poetry run isort .
# Type checking
poetry run mypy simpledsa
Building documentation
cd docs
poetry run make html
The documentation will be available in docs/_build/html.
Publishing a new version
- Update version in pyproject.toml
- Create and push a new tag:
git tag v0.1.1
git push origin v0.1.1
The GitHub Action will automatically build and publish to PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simpledsa-0.2.1.tar.gz.
File metadata
- Download URL: simpledsa-0.2.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.15 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7fb384c9d26f021aab5dd4a32cc0eaa28b1eda0ec9cacc972983e2a32d5f0c38
|
|
| MD5 |
949316146e100cad92c3f47149fccc76
|
|
| BLAKE2b-256 |
e61e5c24d75578ca16c38fb7fc5e6ee554838b60011c4e1c7f3fcfd6170dc3e9
|
File details
Details for the file simpledsa-0.2.1-py3-none-any.whl.
File metadata
- Download URL: simpledsa-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.15 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf1486dce672e8c6a3f06ad6ae5784bae65d9ce51bfe4bdebb3df575283b9b18
|
|
| MD5 |
94be8a898489bf55dc8930c4093066e1
|
|
| BLAKE2b-256 |
accda95bdb431b952938a946082a1a6fc9b0aea4fc531e3a15e158f39e88227a
|