国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python用于學習重要算法的模塊pygorithm實例淺析

2020-01-04 14:42:20
字體:
來源:轉載
供稿:網友

本文實例講述了Python用于學習重要算法的模塊pygorithm。分享給大家供大家參考,具體如下:

這是一個能夠隨時學習重要算法的Python模塊,純粹是為了教學使用。

特點

  • 易于使用
  • 容易理解的文檔
  • 快速獲取算法的源代碼
  • 隨時獲取時間復雜度

安裝

  • 僅需在終端中執行以下命令:
pip3 install pygorithm

*如果你使用的是Python 2.7,請使用pip來安裝。如果存在用戶權限的限制,你可能需要使用pip install --user pygorithm這個命令來安裝。

  • 或者你可以在這里下載源代碼,然后通過以下命令來安裝:
python setup.py install

快速入門

  • 對列表進行排序
from pygorithm.sorting import bubble_sortmyList = [12, 4, 3, 5, 13, 1, 17, 19, 15]sortedList = bubble_sort.sort(myList)print(sortedList)

運行結果:

[1, 3, 4, 5, 12, 13, 15, 17, 19]

  • 獲取當前所用函數的源代碼
from pygorithm.sorting import bubble_sortcode = bubble_sort.get_code()print(code)

運行結果:

def sort(_list):
    """
    Bubble Sorting algorithm

    :param _list: list of values to sort
    :return: sorted values
    """
    for i in range(len(_list)):
        for j in range(len(_list) - 1, i, -1):
            if _list[j] < _list[j - 1]:
                _list[j], _list[j - 1] = _list[j - 1], _list[j]
    return _list

  • 計算某個算法的時間復雜度
from pygorithm.sorting import bubble_sorttime_complexity = bubble_sort.time_complexities()print(time_complexity)

運行結果:

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)

  • 查看模塊中所有有效的函數。例如,如果你想看看排序模塊中所有的排序方法,可以執行以下命令:
>>> from pygorithm.sorting import modules>>> modules()['bubble_sort', 'bucket_sort', 'counting_sort', 'heap_sort', 'insertion_sort', 'merge_sort', 'quick_sort', 'selection_sort', 'shell_sort']

測試

執行以下命令來運行所有的測試用例:

python3 -m unittest

這將運行tests/目錄下的文件中定義的所有測試用例

希望本文所述對大家Python程序設計有所幫助。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汽车| 宁蒗| 和龙市| 宣武区| 临颍县| 云浮市| 封开县| 安化县| 丹巴县| 青州市| 西乌珠穆沁旗| 宜宾市| 南丹县| 永济市| 阜宁县| 吴堡县| 乐至县| 盘锦市| 天等县| 共和县| 开原市| 深州市| 东台市| 永嘉县| 辽宁省| 钟祥市| 江阴市| 香河县| 剑阁县| 大庆市| 巍山| 栾川县| 桦川县| 罗田县| 城固县| 昌黎县| 交城县| 门头沟区| 兴海县| 合阳县| 平安县|