Pygorithm module is a Python module written purely in Python and for educational purposes only. One can get the code, time complexities and much more by just importing the required algorithm. It is a good way to start learning Python programming and understanding concepts. Pygorithm module can also help to learn the implementation of all major algorithms in Python language.
To install Pygorithm module:
pip3 install pygorithm
Example:
# import the required data structurefrom pygorithm.data_structures import stack # create a stack with default stack size 10myStack = stack.Stack() # push elements into the stackmyStack.push(2)myStack.push(5)myStack.push(9)myStack.push(10) # print the contents of stackprint(myStack) # pop elements from stackmyStack.pop()print(myStack) # peek element in stackprint(myStack.peek()) # size of stackprint(myStack.size()) |
Output:
2 5 9 10 2 5 9 9 3
To see all the available functions in a module, just type help() with the module name as argument.
# Help on package pygorithm.data_structureshelp(data_structures) |
Output:
NAME
pygorithm.data_structures - Collection of data structure examples
PACKAGE CONTENTS
graph
heap
linked_list
quadtree
queue
stack
tree
trie
DATA
__all__ = ['graph', 'heap', 'linked_list', 'queue', 'stack', 'tree', '...
To get code for any of these data_structures using get_code().
# to get code for BinarySearchTreeBStree = tree.BinarySearchTree.get_code() print(BStree) |
Output:
class BinarySearchTree(object):
def __init__(self):
self.root = None
def insert(self, data):
"""
inserts a node in the tree
"""
if self.root:
return self.root.insert(data)
else:
self.root = BSTNode(data)
return True
def delete(self, data):
"""
deletes the node with the specified data from the tree
"""
if self.root is not None:
return self.root.delete(data)
def find(self, data):
if self.root:
return self.root.find(data)
else:
return False
def preorder(self):
"""
finding the preorder of the tree
"""
if self.root is not None:
return self.root.preorder(self.root)
def inorder(self):
"""
finding the inorder of the tree
"""
if self.root is not None:
return self.root.inorder(self.root)
def postorder(self):
"""
finding the postorder of the tree
"""
if self.root is not None:
return self.root.postorder(self.root)
@staticmethod
def get_code():
"""
returns the code of the current class
"""
return inspect.getsource(BinarySearchTree)
To get complexities for the following scripts:
# create a stack with default stack size 10Bsort = sorting.bubble_sort.time_complexities() |
Output:
Best Case: O(n), Average Case: O(n ^ 2), Worst Case: O(n ^ 2). For Improved Bubble Sort: Best Case: O(n); Average Case: O(n * (n - 1) / 4); Worst Case: O(n ^ 2)
Quick Link to source code of Pygorithm
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


