Clean, well-documented implementations of common data structures and algorithms in Python. This repository follows a consistent approach throughout all implementations, making it ideal for learning and interview preparation.
- Overview
- Python Version Compatibility
- Installation
- Usage
- Structure of the Repository
- Code Style and Documentation
- Contributing
- Future Improvements
- License
This repository contains implementations of various data structures and algorithms in Python. The code is designed to be:
- Clean and readable: Following PEP 8 style guidelines
- Well-documented: With comprehensive docstrings and comments
- Educational: Focusing on clarity rather than optimization
- Interview-friendly: Covering common interview questions and patterns
The code in this repository is compatible with Python 3.6+. It uses modern Python features such as:
- Type hints (PEP 484)
- F-strings (Python 3.6+)
- Modern class definitions (no need for explicit inheritance from object)
- Up-to-date import conventions
Clone the repository to your local machine:
git clone https://github.com/prabhupant/python-ds.git
cd python-ds
No additional dependencies are required to run the code examples.
Each implementation can be run directly as a Python script. Most files include test cases that demonstrate how to use the implementation.
For example, to run the activity selection algorithm:
python algorithms/greedy/activity_selection.py
You can also import the implementations into your own code:
from data_structures.stack.stack_using_linked_list import Stack, Element
# Create a new stack
stack = Stack()
# Push elements onto the stack
stack.push(Element(1))
stack.push(Element(2))
# Pop an element from the stack
element = stack.pop()
print(element.value) # Output: 2
The repository is organized into three main directories:
Contains implementations of various data structures, categorized by type:
- Array - Array manipulations and operations
- Binary Search Tree - BST implementations and operations
- Binary Trees - Binary tree algorithms
- Circular Linked List - Circular linked list implementations
- Deque - Double-ended queue implementations
- Doubly Linked List - Doubly linked list implementations
- Fenwick Tree - Binary indexed tree implementations
- Graphs - Graph algorithms and representations
- Hash - Hash table implementations
- Heap - Min and max heap implementations
- Linked List - Singly linked list implementations
- Matrix - Matrix operations
- Palindromic Tree - Specialized tree for palindromes
- Queue - Queue implementations
- Segment Tree - Segment tree implementations
- Stack - Stack implementations
- Strings - String manipulation algorithms
- Trie - Trie implementations
- Union Find - Disjoint set data structure
Contains implementations of various algorithms, categorized by type:
- Bit Manipulation - Bit manipulation techniques
- Dynamic Programming - DP solutions to common problems
- Greedy - Greedy algorithm implementations
- Math - Mathematical algorithms
- Miscellaneous - Other algorithm implementations
- Sorting - Sorting algorithm implementations
Contains useful links to external resources, categorized by type:
Category | Link |
---|---|
Articles | Click Here |
Books | Click Here |
Topics | Click Here |
Tutorials | Click Here |
Videos | Click Here |
Misc. | Click Here |
All code in this repository follows the PEP 8 style guide. Each implementation includes:
- A module-level docstring explaining the data structure or algorithm
- Function/method docstrings with parameters and return values
- Type hints for function parameters and return values
- Inline comments explaining complex logic
- Time and space complexity analysis
Example:
def binary_search(arr: List[int], target: int) -> int:
"""
Perform binary search on a sorted array.
Args:
arr: A sorted list of integers
target: The value to search for
Returns:
The index of the target if found, -1 otherwise
Time Complexity: O(log n)
Space Complexity: O(1)
"""
# Implementation details...
Contributions are always welcome! Please read the Contributing Guidelines before submitting a pull request.
Some ways to contribute:
- Add new data structure or algorithm implementations
- Improve existing implementations
- Add test cases
- Fix bugs
- Improve documentation
The repository is continuously evolving. Here are some planned improvements:
- Add more queue implementations and examples
- Expand the algorithms section with more common algorithms
- Add more examples for graph algorithms, trees, heaps, and hash tables
- Add unit tests for all implementations
- Add visualization tools for data structures and algorithms
This project is licensed under the MIT License.