Skip to content

No non-sense and no BS repo for how data structure code should be in Python - simple and elegant.

License

Notifications You must be signed in to change notification settings

thecoder8890/python-ds

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Data Structures and Algorithms

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.

Table of Contents

Overview

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

Python Version Compatibility

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

Installation

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.

Usage

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

Structure of the Repository

The repository is organized into three main directories:

Data Structures

Contains implementations of various data structures, categorized by type:

  1. Array - Array manipulations and operations
  2. Binary Search Tree - BST implementations and operations
  3. Binary Trees - Binary tree algorithms
  4. Circular Linked List - Circular linked list implementations
  5. Deque - Double-ended queue implementations
  6. Doubly Linked List - Doubly linked list implementations
  7. Fenwick Tree - Binary indexed tree implementations
  8. Graphs - Graph algorithms and representations
  9. Hash - Hash table implementations
  10. Heap - Min and max heap implementations
  11. Linked List - Singly linked list implementations
  12. Matrix - Matrix operations
  13. Palindromic Tree - Specialized tree for palindromes
  14. Queue - Queue implementations
  15. Segment Tree - Segment tree implementations
  16. Stack - Stack implementations
  17. Strings - String manipulation algorithms
  18. Trie - Trie implementations
  19. Union Find - Disjoint set data structure

Algorithms

Contains implementations of various algorithms, categorized by type:

  1. Bit Manipulation - Bit manipulation techniques
  2. Dynamic Programming - DP solutions to common problems
  3. Greedy - Greedy algorithm implementations
  4. Math - Mathematical algorithms
  5. Miscellaneous - Other algorithm implementations
  6. Sorting - Sorting algorithm implementations

Bookmarks

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

Code Style and Documentation

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...

Contributing

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

Future Improvements

The repository is continuously evolving. Here are some planned improvements:

  1. Add more queue implementations and examples
  2. Expand the algorithms section with more common algorithms
  3. Add more examples for graph algorithms, trees, heaps, and hash tables
  4. Add unit tests for all implementations
  5. Add visualization tools for data structures and algorithms

License

This project is licensed under the MIT License.

About

No non-sense and no BS repo for how data structure code should be in Python - simple and elegant.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%