From 52ac66260147cd664d605a66d09e253decd3f9a0 Mon Sep 17 00:00:00 2001 From: Divyansh Raj Soni <110761086+DivyanshRajSoni@users.noreply.github.com> Date: Sun, 5 Oct 2025 08:24:21 +0530 Subject: [PATCH 1/4] Update README.md Enhanced README.md file --- README.md | 291 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 288 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 182d36a8d905..4cf4672756e9 100644 --- a/README.md +++ b/README.md @@ -37,16 +37,301 @@

All algorithms implemented in Python - for education ๐Ÿ“š

-Implementations are for learning purposes only. They may be less efficient than the implementations in the Python standard library. Use them at your discretion. +> **Note**: These implementations are for **learning purposes** only. They may be less efficient than the implementations in the Python standard library. Use them at your discretion. + +--- + +### ๐ŸŒŸ Quick Links + +[๐Ÿš€ Get Started](#-getting-started) โ€ข [๐Ÿ“– Browse Algorithms](DIRECTORY.md) โ€ข [๐Ÿ’ป Code Examples](#-usage-examples) โ€ข [๐Ÿค Contribute](CONTRIBUTING.md) โ€ข [๐Ÿ’ฌ Join Discord](https://the-algorithms.com/discord) + +--- + +## ๐Ÿ“‘ Table of Contents + +- [โœจ Why Choose This Repository?](#-why-choose-this-repository) +- [๐Ÿš€ Getting Started](#-getting-started) +- [๐Ÿ“š Algorithm Categories](#-algorithm-categories) +- [๐Ÿ’ก Usage Examples](#-usage-examples) +- [๐Ÿค Contributing](#-contributing) +- [๐ŸŒ Community Channels](#-community-channels) +- [๐ŸŽ“ Learning Path](#-learning-path) +- [๐Ÿ› ๏ธ Development Setup](#๏ธ-development-setup) +- [๐Ÿงช Testing](#-testing) +- [๐Ÿ“Š Project Stats](#-project-stats) +- [๐Ÿ“– Resources](#-resources) +- [๐Ÿ† Top Contributors](#-top-contributors) +- [๐Ÿ“œ License](#-license) +- [๐Ÿ’ฌ Get Help](#-get-help) +- [โญ Show Your Support](#-show-your-support) + +## โœจ Why Choose This Repository? + +- **๐ŸŽ“ Perfect for Learning**: Clean, well-commented code designed specifically for educational purposes +- **๐Ÿ“ˆ 1300+ Implementations**: One of the most comprehensive algorithm collections on GitHub +- **โœ… Quality Assured**: Every algorithm includes tests and passes continuous integration +- **๐ŸŒ Community-Driven**: Thousands of contributors worldwide, actively maintained +- **๐Ÿ“ Well-Documented**: Detailed docstrings, complexity analysis, and usage examples +- **๐Ÿ”ฌ Multiple Domains**: From basic sorting to machine learning, cryptography to computer vision +- **๐Ÿš€ Ready to Use**: Copy, learn from, and adapt code for your projects +- **๐ŸŽฏ Interview Prep**: Perfect resource for coding interviews and competitive programming ## ๐Ÿš€ Getting Started +### Prerequisites + +- Python 3.8 or higher +- pip (Python package installer) + +### Installation + +1. Clone the repository: +```bash +git clone https://github.com/TheAlgorithms/Python.git +cd Python +``` + +2. Install dependencies: +```bash +pip install -r requirements.txt +``` + +3. Run any algorithm: +```bash +python3 sorts/quick_sort.py +python3 searches/binary_search.py +``` + +### Running Tests + +```bash +pytest +``` + +## ๐Ÿ“š Algorithm Categories + + + + + + +
+ +**Computer Science Fundamentals** +- ๐Ÿ—‚๏ธ Data Structures (Arrays, Trees, Graphs, Heaps) +- ๐Ÿ”„ Sorting (Quick, Merge, Heap, Radix, etc.) +- ๐Ÿ” Searching (Binary, Ternary, Jump, etc.) +- ๐Ÿ”™ Backtracking (N-Queens, Sudoku, Maze) +- ๐ŸŒŠ Divide and Conquer +- ๐Ÿ’ฐ Dynamic Programming +- ๐ŸŽฏ Greedy Algorithms +- ๐Ÿ“Š Graph Algorithms (BFS, DFS, Dijkstra, Floyd-Warshall) + +**Mathematics & Science** +- โž• Mathematical Algorithms +- ๐Ÿ”ข Number Theory +- ๐Ÿ“ Geometry & Computational Geometry +- ๐Ÿงฎ Linear Algebra & Matrix Operations +- ๐Ÿ“ˆ Statistics & Probability +- โš›๏ธ Physics Simulations +- ๐ŸŒ Geodesy + + + +**Machine Learning & AI** +- ๐Ÿค– Machine Learning (Regression, Classification) +- ๐Ÿง  Neural Networks +- ๐Ÿงฌ Genetic Algorithms +- ๐ŸŽฒ Fuzzy Logic +- ๐Ÿ“Š Data Mining + +**Applied Computer Science** +- ๐Ÿ” Cryptography & Ciphers +- ๐Ÿ–ผ๏ธ Computer Vision +- ๐ŸŽจ Digital Image Processing +- ๐ŸŽต Audio Filters +- ๐ŸŒ Web Programming +- ๐Ÿ“ก Networking & Data Transfer +- ๐Ÿ—œ๏ธ Data Compression +- ๐Ÿ’น Financial Algorithms +- โšก Electronics & Circuit Design + +
+ +๐Ÿ“– See our complete **[DIRECTORY.md](DIRECTORY.md)** for the full list of all 1300+ implementations. + +## ๐Ÿ’ก Usage Examples + +### Quick Start - Sorting +```python +from sorts.quick_sort import quick_sort + +arr = [64, 34, 25, 12, 22, 11, 90] +sorted_arr = quick_sort(arr) +print(sorted_arr) # [11, 12, 22, 25, 34, 64, 90] +``` + +### Binary Search +```python +from searches.binary_search import binary_search + +sorted_list = [1, 3, 5, 7, 9, 11, 13, 15, 17] +target = 7 +index = binary_search(sorted_list, target) +print(f"Found {target} at index {index}") # Found 7 at index 3 +``` + +### Graph Algorithms - Dijkstra's Shortest Path +```python +from graphs.dijkstra import dijkstra + +# Graph represented as adjacency list +graph = { + 'A': {'B': 1, 'C': 4}, + 'B': {'C': 2, 'D': 5}, + 'C': {'D': 1}, + 'D': {} +} + +distances = dijkstra(graph, 'A') +print(distances) # {'A': 0, 'B': 1, 'C': 3, 'D': 4} +``` + +### Machine Learning - Linear Regression +```python +from machine_learning.linear_regression import LinearRegression + +X = [[1], [2], [3], [4], [5]] +y = [2, 4, 6, 8, 10] + +model = LinearRegression() +model.fit(X, y) +prediction = model.predict([[6]]) +print(prediction) # ~12 +``` + +### Cryptography - Caesar Cipher +```python +from ciphers.caesar_cipher import encrypt, decrypt + +message = "HELLO WORLD" +encrypted = encrypt(message, shift=3) +print(encrypted) # "KHOOR ZRUOG" + +decrypted = decrypt(encrypted, shift=3) +print(decrypted) # "HELLO WORLD" +``` + +## ๐Ÿค Contributing + +We love contributions! This project exists thanks to all the people who contribute. + ๐Ÿ“‹ Read through our [Contribution Guidelines](CONTRIBUTING.md) before you contribute. +### How to Contribute + +1. Fork the repository +2. Create a new branch (`git checkout -b feature/algorithm-name`) +3. Make your changes and commit (`git commit -am 'Add new algorithm'`) +4. Push to the branch (`git push origin feature/algorithm-name`) +5. Create a Pull Request + ## ๐ŸŒ Community Channels We are on [Discord](https://the-algorithms.com/discord) and [Gitter](https://gitter.im/TheAlgorithms/community)! Community channels are a great way for you to ask questions and get help. Please join us! -## ๐Ÿ“œ List of Algorithms +## ๐ŸŽ“ Learning Path + +New to algorithms? Follow this recommended learning path: + +1. **Start with Basics**: `sorts/` โ†’ `searches/` โ†’ `data_structures/` +2. **Build Foundation**: `recursion/` โ†’ `backtracking/` โ†’ `divide_and_conquer/` +3. **Advanced Topics**: `dynamic_programming/` โ†’ `graphs/` โ†’ `greedy_methods/` +4. **Specialized Areas**: `machine_learning/` โ†’ `ciphers/` โ†’ `neural_network/` + +Each directory contains a README with explanations and complexity analysis. + +## ๐Ÿ› ๏ธ Development Setup + +### Using Virtual Environment +```bash +# Create virtual environment +python -m venv venv + +# Activate it +source venv/bin/activate # On Windows: venv\Scripts\activate + +# Install dependencies +pip install -r requirements.txt +``` + +### Using pre-commit hooks +```bash +pip install pre-commit +pre-commit install +``` + +This will automatically format your code and run checks before each commit. + +## ๐Ÿงช Testing + +```bash +# Run all tests +pytest + +# Run tests for a specific module +pytest sorts/test_sorts.py -See our [directory](DIRECTORY.md) for easier navigation and a better overview of the project. +# Run with coverage +pytest --cov=. --cov-report=html +``` + +## ๐Ÿ“Š Project Stats + +- **Total Implementations**: 1300+ algorithms +- **Lines of Code**: 100,000+ +- **Contributors**: 1000+ +- **Stars**: Check the repo! +- **Programming Language**: Python 3.8+ + +## ๐Ÿ“– Resources + +- ๐ŸŒ [The Algorithms Website](https://the-algorithms.com/) +- ๐Ÿ“š [Python Algorithm Documentation](https://the-algorithms.com/language/python) +- ๐Ÿ“ [Contributing Guide](CONTRIBUTING.md) +- ๐Ÿค [Code of Conduct](https://github.com/TheAlgorithms/.github/blob/master/CODE_OF_CONDUCT.md) +- ๐ŸŽฅ [Video Tutorials](https://www.youtube.com/c/thealgorithms) + +## ๐Ÿ† Top Contributors + +A huge thanks to all our contributors! This project wouldn't be possible without you. + + + + + +## ๐Ÿ“œ License + +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. + +## ๐Ÿ’ฌ Get Help + +- ๐Ÿ“ซ Create an [Issue](https://github.com/TheAlgorithms/Python/issues/new/choose) for bug reports or feature requests +- ๐Ÿ’ญ Join our [Discord](https://the-algorithms.com/discord) for discussions +- ๐Ÿ—จ๏ธ Ask questions on [Gitter](https://gitter.im/TheAlgorithms/community) + +## โญ Show Your Support + +If you find this project helpful: +- Give it a โญ star on GitHub +- Share it with your friends and colleagues +- Contribute by adding new algorithms or improving existing ones +- Help us translate documentation + +--- + +
+ Made with โค๏ธ by contributors around the ๐ŸŒ +
+ Happy Coding! ๐Ÿš€ +
From 6145c078d119d576dce3b5f3a5d99af307fae12f Mon Sep 17 00:00:00 2001 From: Divyansh Raj Soni <110761086+DivyanshRajSoni@users.noreply.github.com> Date: Sun, 5 Oct 2025 08:28:54 +0530 Subject: [PATCH 2/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4cf4672756e9..9ec57e4a28df 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -
+
From 3a6c6f68c6f067ee1ce5c95945f3107d1990f878 Mon Sep 17 00:00:00 2001 From: Divyansh Raj Soni <110761086+DivyanshRajSoni@users.noreply.github.com> Date: Mon, 6 Oct 2025 12:10:57 +0530 Subject: [PATCH 3/4] Add Catalan number algorithm with three implementations Implements Catalan number calculation using: - Dynamic programming approach - Recursive with memoization - Binomial coefficient formula Includes 18 passing doctests, type hints, and comprehensive documentation. --- maths/catalan_number.py | 175 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 maths/catalan_number.py diff --git a/maths/catalan_number.py b/maths/catalan_number.py new file mode 100644 index 000000000000..d0b82184ae8e --- /dev/null +++ b/maths/catalan_number.py @@ -0,0 +1,175 @@ +""" +Catalan Numbers + +Catalan numbers form a sequence of natural numbers that occur in various counting +problems in combinatorics. The nth Catalan number can be expressed directly in +terms of binomial coefficients. + +Formula: C(n) = (2n)! / ((n + 1)! * n!) + +Alternative formula: C(n) = C(n-1) * 2(2n-1) / (n+1) + +Applications: +- Number of different ways n + 1 factors can be completely parenthesized +- Number of different binary search trees with n keys +- Number of paths with n steps east and n steps north that don't cross diagonal +- Number of ways to triangulate a polygon with n + 2 sides + +Reference: https://en.wikipedia.org/wiki/Catalan_number +""" + + +def catalan_number(node_count: int) -> int: + """ + Calculate the nth Catalan number using dynamic programming approach. + + Args: + node_count: A non-negative integer representing the position in sequence + + Returns: + The nth Catalan number + + Raises: + ValueError: If node_count is negative + + Examples: + >>> catalan_number(0) + 1 + >>> catalan_number(1) + 1 + >>> catalan_number(5) + 42 + >>> catalan_number(10) + 16796 + >>> catalan_number(15) + 9694845 + >>> catalan_number(-1) + Traceback (most recent call last): + ... + ValueError: node_count must be a non-negative integer + >>> catalan_number(3.5) + Traceback (most recent call last): + ... + ValueError: node_count must be a non-negative integer + """ + if not isinstance(node_count, int) or node_count < 0: + raise ValueError("node_count must be a non-negative integer") + + if node_count <= 1: + return 1 + + # Dynamic programming approach + catalan = [0] * (node_count + 1) + catalan[0] = catalan[1] = 1 + + for i in range(2, node_count + 1): + for j in range(i): + catalan[i] += catalan[j] * catalan[i - 1 - j] + + return catalan[node_count] + + +def catalan_number_recursive(node_count: int) -> int: + """ + Calculate the nth Catalan number using recursive formula with memoization. + + Args: + node_count: A non-negative integer representing the position in sequence + + Returns: + The nth Catalan number + + Raises: + ValueError: If node_count is negative + + Examples: + >>> catalan_number_recursive(0) + 1 + >>> catalan_number_recursive(1) + 1 + >>> catalan_number_recursive(5) + 42 + >>> catalan_number_recursive(10) + 16796 + >>> catalan_number_recursive(-1) + Traceback (most recent call last): + ... + ValueError: node_count must be a non-negative integer + """ + if not isinstance(node_count, int) or node_count < 0: + raise ValueError("node_count must be a non-negative integer") + + memo: dict[int, int] = {} + + def helper(n: int) -> int: + if n <= 1: + return 1 + if n in memo: + return memo[n] + + result = 0 + for i in range(n): + result += helper(i) * helper(n - 1 - i) + + memo[n] = result + return result + + return helper(node_count) + + +def catalan_number_binomial(node_count: int) -> int: + """ + Calculate the nth Catalan number using binomial coefficient formula. + + Formula: C(n) = (2n)! / ((n + 1)! * n!) + which equals: C(2n, n) / (n + 1) + + Args: + node_count: A non-negative integer representing the position in sequence + + Returns: + The nth Catalan number + + Raises: + ValueError: If node_count is negative + + Examples: + >>> catalan_number_binomial(0) + 1 + >>> catalan_number_binomial(1) + 1 + >>> catalan_number_binomial(5) + 42 + >>> catalan_number_binomial(10) + 16796 + >>> catalan_number_binomial(15) + 9694845 + >>> catalan_number_binomial(-1) + Traceback (most recent call last): + ... + ValueError: node_count must be a non-negative integer + """ + if not isinstance(node_count, int) or node_count < 0: + raise ValueError("node_count must be a non-negative integer") + + if node_count <= 1: + return 1 + + # Calculate binomial coefficient C(2n, n) + result = 1 + for i in range(node_count): + result = result * (2 * node_count - i) // (i + 1) + + # Divide by (n + 1) + return result // (node_count + 1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + # Print first 15 Catalan numbers + print("First 15 Catalan numbers:") + for i in range(15): + print(f"C({i}) = {catalan_number(i)}") From 374b63fa7d5db04d80ae0d81caf9bd10b9ef7dfa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 06:53:51 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ec57e4a28df..4cf4672756e9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -
+