Pygorithm module 是一个纯粹用 Python 编写的 Python 模块,仅用于教育目的。只需导入所需的算法即可获得代码、时间复杂度等等。这是开始学习 Python 编程和理解概念的好方法。 Pygorithm模块还可以帮助学习Python语言中所有主要算法的实现。
要安装 Pygorithm 模块:
pip3 install pygorithm
例子:
# import the required data structure
from pygorithm.data_structures import stack
# create a stack with default stack size 10
myStack = stack.Stack()
# push elements into the stack
myStack.push(2)
myStack.push(5)
myStack.push(9)
myStack.push(10)
# print the contents of stack
print(myStack)
# pop elements from stack
myStack.pop()
print(myStack)
# peek element in stack
print(myStack.peek())
# size of stack
print(myStack.size()) 输出:
2 5 9 10 2 5 9 9 3
要查看模块中的所有可用函数,只需键入help()以模块名称作为参数。
# Help on package pygorithm.data_structures
help(data_structures) 输出:
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', '...
使用get_code()获取任何data_structures的代码。
# to get code for BinarySearchTree
BStree = tree.BinarySearchTree.get_code()
print(BStree) 输出:
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)
要了解以下脚本的复杂性:
# create a stack with default stack size 10
Bsort = sorting.bubble_sort.time_complexities() 输出:
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)
相关用法
- Python Pyglet和Pygame的区别用法及代码示例
- Python PyTorch acos()用法及代码示例
- Python PyTorch asin()用法及代码示例
- Python PyTorch atan()用法及代码示例
- Python PyTorch cos()用法及代码示例
- Python PyTorch cosh()用法及代码示例
- Python PyTorch sin()用法及代码示例
- Python PyTorch sinh()用法及代码示例
- Python PyTorch tan()用法及代码示例
- Python PyTorch tanh()用法及代码示例
- Python PyTorch from_numpy()用法及代码示例
- Python Pytorch randn()用法及代码示例
- Python Pytorch permute()用法及代码示例
- Python PyTorch div()用法及代码示例
- Python PyTorch clamp()用法及代码示例
- Python PyTorch ceil()用法及代码示例
- Python PyTorch add()用法及代码示例
- Python PyTorch abs()用法及代码示例
- Python PyTorch exp()用法及代码示例
- Python PyTorch numel()用法及代码示例
- Python PyTorch is_storage()用法及代码示例
- Python PyTorch is_tensor()用法及代码示例
- Python PyTorch trunc()用法及代码示例
- Python PyTorch frac()用法及代码示例
- Python PyTorch log()用法及代码示例
注:本文由纯净天空筛选整理自Shantanu Sharma.大神的英文原创作品 Pygorithm module in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
