Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Module Pygorithm trong Python


Module Pygorithm là mô-đun giáo dục chứa triển khai các thuật toán. Ưu điểm lớn nhất là cung cấp mã nguồn thuật toán bằng Python. Cũng có thể dùng cho lập trình thực tế bằng cách áp dụng thuật toán vào dữ liệu.

Tìm cấu trúc dữ liệu

Sau khi cài đặt, có thể xem các cấu trúc dữ liệu có sẵn trong package.

Ví dụ

from pygorithm import data_structures
help(data_structures)

Kết quả:

Đầu ra

Help on package pygorithm.data_structures in pygorithm:
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', 'trie', ...]

Lấy mã thuật toán

Ví dụ lấy mã của cấu trúc dữ liệu Queue.

Ví dụ

from pygorithm.data_structures.queue import Queue
the_Queue = Queue()
print(the_Queue.get_code())

Kết quả (rút gọn):

Đầu ra

class Queue(object):
    """Queue implementation"""
    def __init__(self, limit=10):
        self.queue = []
        self.front = None
        self.rear = None
        self.limit = limit
        self.size = 0
    ...............

Áp dụng sắp xếp

Ví dụ áp dụng Quick Sort cho danh sách.

Ví dụ

from pygorithm.sorting import quick_sort
my_list = [3,9,5,21,2,43,18]
sorted_list = quick_sort.sort(my_list)
print(sorted_list)

Kết quả:

Đầu ra

[2, 3, 5, 9, 18, 21, 43]