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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
