LeetPy
v0.2.1
調試是首先寫代碼的兩倍。
如果您解決了與數據結構和算法(DSA)有關的問題,則您將知道對複雜的數據結構(如二進制樹和有向圖)進行調試是多麼令人沮喪。
LeetPy是一個輕巧的Python軟件包,在解決DSA問題時會使您更有效。它包含實用程序功能和算法,使調試和測試變得更加容易。這是一些功能:
LeetPy提供了方便的print()功能,以顯示您的結構的外觀(所有終端內部!)。LeetPy的算法始終可以正常工作。 要安裝最新的穩定版本,請運行:
$ pip install leetpy從最新的github提交中安裝:
pip install git+https://github.com/aryanpingle/leetpy這是一個最少的用例:
# Create a random binary tree and visualize it
from leetpy import BinaryTree
root = BinaryTree . create ( n = 20 ) # create a random binary tree with 20 nodes
BinaryTree . print_structure ( root ) # visualize the binary tree這是一個複雜的:
# Suppose you want to 'save' 10 binary search trees (example: for testing purposes)
# You would need some Python code that generates each tree exactly
from leetpy import BinaryTree
for i in range ( 1 , 11 ):
# Generate a random binary search tree (BST) with 20 nodes
# Each node should have a value between 1 and 10 (inclusive)
root = BinaryTree . create ( n = 20 , min_val = 1 , max_val = 10 , make_bst = True )
# Get the python code that generates this exact BST
# Oh, and make each node an object of class "CustomNode"
# Oh, and keep indentation to 2 spaces
code += " n " + BinaryTree . export_as_code ( root , node_alias = "CustomNode" , indent = 2 )
with open ( "testing.py" , "w" ) as f :
f . write ( code ) LeetPy為廣泛的數據結構提供了廣泛的實用程序功能。有關使用示例的全面列表,請訪問 /examples/readme.md。
LeetPy計劃支持以下數據結構:
所有數據結構都有一些常見的API:
create() -> structure - 基於某些參數創建帶有隨機數據和屬性的結構export_as_code(structure) -> str獲得一個獨立的python3函數,當調用時,返回給定的數據結構export_as_svg(structure) -> None創建具有給定數據結構的SVG文件print(structure) -> None - 打印數據結構的表示終端