This repo is a collection of small data structure examples I made while practicing core CS topics.
I wanted the examples to feel more useful than just textbook code, so each one is connected to a normal software problem.
| File | Data Structure | Example Use |
|---|---|---|
lru_cache.py |
dictionary + doubly linked list | keeping recently used values |
dependency_resolver.py |
graph | ordering tasks with prerequisites |
autocomplete.py |
trie | finding words from a prefix |
priority_task_queue.py |
heap | picking the most important task first |
union_find.py |
disjoint set | checking which items are connected |
I wanted a GitHub project that shows I understand data structures, but in a way that is still practical.
For example:
- caches need fast lookup
- autocomplete needs prefix search
- task queues need priorities
- dependency planners need graph traversal
- connected groups can be handled with union-find
Run the tests:
PYTHONPATH=src python -m unittest discover -s testsRun the demo:
PYTHONPATH=src python examples/demo.pyfrom practical_ds.lru_cache import LRUCache
cache = LRUCache(capacity=2)
cache.put("user:1", "Saina")
cache.put("user:2", "Ari")
cache.get("user:1")
cache.put("user:3", "Mina")
assert cache.get("user:2") is NoneHere, user:2 gets removed because it was the least recently used item.
- writing clean Python classes
- using tests to check behavior
- thinking about time complexity
- connecting data structures to real examples
- making a repo that is easy to run