Top Related Projects
My Python Examples
Minimal examples of data structures and algorithms in Python
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.
:computer: Data Structures and Algorithms in Python
Quick Overview
Pygorithm is a Python library that provides implementations of various algorithms and data structures. It aims to help developers and students learn and understand different algorithms by providing clean, well-documented code examples along with explanations of time and space complexity.
Pros
- Comprehensive collection of algorithms and data structures
- Well-documented code with explanations of time and space complexity
- Easy to use and understand for educational purposes
- Includes visualization tools for some algorithms
Cons
- Not optimized for production use
- Limited to Python implementations only
- Some algorithms may not be updated to the most efficient versions
- Lacks advanced or specialized algorithms in certain areas
Code Examples
- Finding the nth Fibonacci number:
from pygorithm.fibonacci import fibonacci
result = fibonacci.get_sequence(10)
print(result) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
- Performing binary search:
from pygorithm.searching import binary_search
result = binary_search.search([1, 2, 3, 4, 5, 6, 7], 5)
print(result) # Output: 4 (index of the found element)
- Sorting an array using quicksort:
from pygorithm.sorting import quick_sort
result = quick_sort.sort([64, 34, 25, 12, 22, 11, 90])
print(result) # Output: [11, 12, 22, 25, 34, 64, 90]
Getting Started
To get started with Pygorithm, follow these steps:
-
Install the library using pip:
pip install pygorithm
-
Import the desired module in your Python script:
from pygorithm.sorting import bubble_sort
-
Use the imported module:
my_list = [64, 34, 25, 12, 22, 11, 90] sorted_list = bubble_sort.sort(my_list) print(sorted_list)
-
To get the code and time complexity of an algorithm:
print(bubble_sort.get_code()) print(bubble_sort.time_complexities())
Competitor Comparisons
My Python Examples
Pros of Python
- Broader scope, covering various Python topics and utilities
- Larger collection of scripts and examples
- More active community with frequent contributions
Cons of Python
- Less organized structure compared to Pygorithm
- Lacks focused documentation on algorithm implementations
- May be overwhelming for beginners looking for specific algorithms
Code Comparison
Pygorithm (Binary Search implementation):
def binary_search(sorted_collection, item):
left = 0
right = len(sorted_collection) - 1
while left <= right:
midpoint = left + (right - left) // 2
current_item = sorted_collection[midpoint]
if current_item == item:
return midpoint
elif item < current_item:
right = midpoint - 1
else:
left = midpoint + 1
return None
Python (Binary Search implementation):
def binary_search(lys, val):
first = 0
last = len(lys)-1
while first <= last:
mid = (first + last)//2
if lys[mid] == val:
return mid
elif lys[mid] > val:
last = mid - 1
else:
first = mid + 1
return -1
Both implementations are similar, with minor differences in variable naming and return values for unsuccessful searches.
Minimal examples of data structures and algorithms in Python
Pros of algorithms
- More comprehensive coverage of algorithms and data structures
- Better organized with clear categorization of topics
- More active development and community contributions
Cons of algorithms
- Less focus on educational aspects and explanations
- Lacks built-in visualization tools for algorithm demonstrations
Code Comparison
algorithms:
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
pygorithm:
def binary_search(List, key):
left = 0
right = len(List) - 1
while left <= right:
mid = (left + right) // 2
if List[mid] == key:
return mid
elif List[mid] > key:
right = mid - 1
else:
left = mid + 1
return -1
Both implementations are similar, with algorithms using more descriptive variable names (arr, target) compared to pygorithm (List, key). The overall structure and logic remain the same in both cases.
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
Pros of javascript-algorithms
- More comprehensive coverage of algorithms and data structures
- Better organized with clear categorization and README files for each topic
- Includes Big O complexity analysis for each algorithm
Cons of javascript-algorithms
- Lacks built-in testing functionality, unlike pygorithm's integrated testing
- Does not provide a command-line interface for easy algorithm execution
- Less focus on educational explanations compared to pygorithm's detailed comments
Code Comparison
pygorithm (Python):
def bubble_sort(List):
for i in range(len(List)):
for j in range(len(List) - 1, i, -1):
if List[j] < List[j - 1]:
List[j], List[j - 1] = List[j - 1], List[j]
return List
javascript-algorithms (JavaScript):
function bubbleSort(originalArray) {
const array = [...originalArray];
for (let i = 0; i < array.length; i += 1) {
for (let j = 0; j < array.length - i - 1; j += 1) {
if (array[j] > array[j + 1]) {
[array[j], array[j + 1]] = [array[j + 1], array[j]];
}
}
}
return array;
}
Both implementations showcase the bubble sort algorithm, with javascript-algorithms using more modern JavaScript features like spread operator and destructuring assignment.
120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.
Pros of interactive-coding-challenges
- More comprehensive coverage of data structures and algorithms
- Includes interactive Jupyter notebooks for hands-on learning
- Provides test cases and solutions for each challenge
Cons of interactive-coding-challenges
- Less focused on providing a library of ready-to-use algorithms
- May be more challenging for beginners due to its depth and complexity
- Requires more setup (Jupyter notebooks) compared to a simple Python package
Code Comparison
pygorithm:
from pygorithm.sorting import bubble_sort
my_list = [12, 4, 3, 5, 13, 1, 17, 19, 15]
sorted_list = bubble_sort.sort(my_list)
interactive-coding-challenges:
def bubble_sort(data):
for _ in range(len(data)):
for i in range(1, len(data)):
if data[i-1] > data[i]:
data[i-1], data[i] = data[i], data[i-1]
return data
The interactive-coding-challenges repository focuses on providing a comprehensive set of coding challenges with solutions and explanations, making it ideal for interview preparation and in-depth learning. In contrast, pygorithm is designed as a library of algorithms that can be easily imported and used in Python projects, with a focus on simplicity and ease of use.
:computer: Data Structures and Algorithms in Python
Pros of Algorithms
- More comprehensive coverage of algorithms and data structures
- Implementations in multiple programming languages (Python, C++, Java)
- Includes advanced topics like graph algorithms and dynamic programming
Cons of Algorithms
- Less focus on educational aspects and explanations
- Not actively maintained (last commit over 3 years ago)
- Lacks a structured package or module system
Code Comparison
Pygorithm (Binary Search implementation):
def binary_search(sorted_collection, item):
left = 0
right = len(sorted_collection) - 1
while left <= right:
midpoint = left + (right - left) // 2
current_item = sorted_collection[midpoint]
if current_item == item:
return midpoint
elif item < current_item:
right = midpoint - 1
else:
left = midpoint + 1
return None
Algorithms (Binary Search implementation):
def binary_search(arr, x):
l, r = 0, len(arr) - 1
while l <= r:
mid = (l + r) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
l = mid + 1
else:
r = mid - 1
return -1
Both implementations are similar in approach, but Pygorithm's version includes more descriptive variable names and uses integer division explicitly.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Pygorithm
.. image:: https://img.shields.io/packagist/l/doctrine/orm.svg :target: https://github.com/OmkarPathak/pygorithm/blob/master/LICENSE :alt: Packagist
.. image:: http://pepy.tech/badge/pygorithm :target: http://pepy.tech/project/pygorithm :alt: Downloads
.. image:: https://readthedocs.org/projects/pygorithm/badge/?version=latest :target: http://pygorithm.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status
.. image:: https://img.shields.io/badge/Python-3.6-brightgreen.svg :target: https://github.com/OmkarPathak/pygorithm :alt: Python 3.6
.. image:: https://img.shields.io/badge/Say%20Thanks-%F0%9F%A6%89-1EAEDB.svg :target: https://saythanks.io/to/omkarpathak27@gmail.com :alt: Say Thanks!
.. image:: https://img.shields.io/github/contributors/omkarpathak/pygorithm.svg :target: https://github.com/OmkarPathak/pygorithm/graphs/contributors :alt: Contributors
| A Python module to learn all the major algorithms on the go! | Purely for educational purposes
.. image:: https://images.gitads.io/pygorithm :target: https://tracking.gitads.io/?campaign=gitads&repo=pygorithm&redirect=gitads.io
Features
* Super easy to use
* A very easy to understand `Documentation <http://pygorithm.readthedocs.io/en/latest/>`_
* Get the code right in your editor
* Get time complexities on the go
Installation
- Just fire the following command in your terminal:
::
pip3 install pygorithm
- | It's that easy. If you are using Python 2.7 use pip instead. Depending on your
| permissions, you might need to use
pip install --user pygorithm
to install.
- Or you can download the source code from
here <https://github.com/OmkarPathak/pygorithm>
_, and then just install the package using
::
python setup.py install
Quick Start Guide
* To sort your list
.. code:: python
>>> from pygorithm.sorting import bubble_sort
>>> my_list = [12, 4, 3, 5, 13, 1, 17, 19, 15]
>>> sorted_list = bubble_sort.sort(my_list)
>>> print(sorted_list)
>>> [1, 3, 4, 5, 12, 13, 15, 17, 19]
* To get the code for function used
.. code:: python
>>> from pygorithm.sorting import bubble_sort
>>> code = bubble_sort.get_code()
>>> print(code)
* To get the time complexity of an algorithm
.. code:: python
>>> from pygorithm.sorting import bubble_sort
>>> time_complexity = bubble_sort.time_complexities()
>>> print(time_complexity)
* To see all the available functions in a module, you can just type ``help()`` with the module name as argument. For example,
.. code:: python
>>> from pygorithm import sorting
>>> help(sorting)
Help on package pygorithm.sorting in pygorithm:
NAME
pygorithm.sorting - Collection of sorting methods
PACKAGE CONTENTS
bubble_sort
bucket_sort
counting_sort
heap_sort
insertion_sort
merge_sort
modules
quick_sort
selection_sort
shell_sort
Tests
~~~~~
* Just type in the following command to run the tests
::
python3 -m unittest
* This will run all the tests defined in the files of the ``tests/`` directory
Donation
~~~~~~~~
If you have found my softwares to be of any use to you, do consider helping me pay my internet bills. This would encourage me to create many such softwares :)
- `PayPal <https://paypal.me/omkarpathak27>`_
- `â¹ (INR) <https://www.instamojo.com/@omkarpathak/>`_
Top Related Projects
My Python Examples
Minimal examples of data structures and algorithms in Python
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.
:computer: Data Structures and Algorithms in Python
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot