📜  Python中的Pygorithm模块

📅  最后修改于: 2022-05-13 01:54:24.908000             🧑  作者: Mango

Python中的Pygorithm模块

Pygorithm 模块是一个纯粹用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() 获取任何这些数据结构的代码。

# 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)

快速链接到 Pygorithm 的源代码