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

首頁 > 編程 > Python > 正文

Python pygorithm模塊用法示例【常見算法測試】

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

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

pygorithm:一個用純粹python編寫的Python模塊,用于純粹的教育目的。只需導入所需的算法即可獲取代碼,時間復雜度等等。開始學習Python編程的好方法。了解Python中所有主要算法的實現。不需要上網就可以獲得所需的代碼。

安裝

pip3 install pygorithm

常見函數

斐波那契數列

from pygorithm.fibonacci import recursionresult = recursion.get_sequence(10)print(result)    # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]code = recursion.get_code()   # 獲取實現函數的算法print(code)

獲取最小公倍數

from pygorithm.math import lcmresult = lcm.lcm([4,6])print(result)    # 12code = lcm.get_code()      # 獲取實現函數的算法print(code)

質數算法

from pygorithm.math import sieve_of_eratosthenesresult = sieve_of_eratosthenes.sieve_of_eratosthenes(10)  # 獲取小于10的質數print(result)    # [2,3,5,7]code = lcm.get_code()      # 獲取實現函數的算法print(code)

階乘

from pygorithm.math import factorialresult = factorial.factorial(5)   # 獲取5的階乘,即1*2*3*4*5print(result)    # 120code = factorial.get_code()   # 獲取實現函數的算法print(code)

十進制轉二進制

from pygorithm.math import conversionresult = conversion.decimal_to_binary(3)  # 將3轉換為二進制print(result)    # 11code = conversion.get_code()  # 獲取實現函數的算法print(code)

二進制轉十進制

from pygorithm.math import conversionresult = conversion.binary_to_decimal(11)  # 將11轉換為十進制print(result)    # 3code = conversion.get_code()  # 獲取實現函數的算法print(code)

十進制轉十六進制

from pygorithm.math import conversionresult = conversion.decimal_to_hex(15)   # 將15轉換為十六進制數print(result)    # Fcode = conversion.get_code()  # 獲取實現函數的算法print(code)

十六進制轉十進制

from pygorithm.math import conversionresult = conversion.hex_to_decimal("F")   # 將十六進制F轉化為十進制數print(result)    # 15code = conversion.get_code()  # 獲取實現函數的算法print(code)

二分法搜索:效率高

from pygorithm.searching import binary_searchl = [9,4,5,1,7]index = binary_search.search(l,5)   # 獲取5在列表中的位置,找到返回下標,找不到返回Falseprint(index)code = binary_search.get_code() # 獲取實現函數的算法print(code)

線性搜索:速度慢,適用性廣

from pygorithm.searching import linear_searchl = [9,4,5,1,7]index = linear_search.search(l,5)    # 獲取5在列表中的位置,找到返回下標,找不到返回Falseprint(index)code = linear_search.get_code() # 獲取實現函數的算法print(code)

插值搜索:注意:列表必須先經過升序排序,否則將找不到

from pygorithm.searching import interpolation_searchl = [1,4,5,7,9]index = interpolation_search.search(l,4)  # 獲取5在列表中的位置,找到返回下標,找不到返回Falseprint(index)code = interpolation.get_code() # 獲取實現函數的算法print(code)

冒泡排序

from pygorithm.sorting import bubble_sortl = [9,4,5,1,7]result = bubble_sort.sort(l)print(result)    # [1, 4, 5, 7, 9]code = bubble_sort.get_code()  # 獲取實現函數的算法print(code)

改良冒泡排序

from pygorithm.sorting import bubble_sortl = [9,4,5,1,7]result = bubble_sort.improved_sort(l)print(result)    # [1, 4, 5, 7, 9]

桶排序

from pygorithm.sorting import bucket_sortl = [9,4,5,1,7]result = bucket_sort.sort(l,5) # 5為桶的大小,默認為5print(result)    # [1, 4, 5, 7, 9]code = bucket_sort.get_code()  # 獲取實現函數的算法print(code)

計數排序

from pygorithm.sorting import counting_sortl = [9,4,5,1,7]result = counting_sort.sort(l) print(result)    # [1, 4, 5, 7, 9]code = counting_sort.get_code() # 獲取實現函數的算法print(code)

堆排序

from pygorithm.sorting import heap_sortl = [9,4,5,1,7]result = heap_sort.sort(l)print(result)    # [1, 4, 5, 7, 9]code = heap_sort.get_code()   # 獲取實現函數的算法print(code)

插入排序

from pygorithm.sorting import insertion_sortl = [9,4,5,1,7]result = insertion_sort(l)print(result)    # [1, 4, 5, 7, 9]code = insertion_sort.get_code()  # 獲取實現函數的算法print(code)

歸并排序

from pygorithm.sorting import merge_sortl = [9,4,5,1,7]result = merge_sort.sort(l)print(result)    # [1, 4, 5, 7, 9]code = merge_sort.get_code()    # 獲取實現函數的算法print(code)

快速排序

from pygorithm.sorting import quick_sortl = [9,4,5,1,7]result = quick_sort.sort(l)print(result)    # [1, 4, 5, 7, 9]code = quick_sort.get_code()    # 獲取實現函數的算法print(code)

選擇排序

from pygorithm.sorting import selection_sortl = [9,4,5,1,7]result = selection_sort.sort(l)print(result)    # [1, 4, 5, 7, 9]code = selection_sort.get_code()  # 獲取實現函數的算法print(code)

希爾排序

from pygorithm.sorting import shell_sortl = [9,4,5,1,7]result = shell_sort.sort(l)print(result)    # [1, 4, 5, 7, 9]code = shell_sort.get_code()    # 獲取實現函數的算法print(code)

更多經典算法: http://pygorithm.readthedocs.io/en/latest/index.html

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


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福安市| 大田县| 竹溪县| 上林县| 宁都县| 东台市| 彰化县| 诸城市| 留坝县| 达日县| 榆社县| 汉阴县| 聂荣县| 贞丰县| 县级市| 渭源县| 东安县| 南京市| 民乐县| 九龙县| 贵溪市| 屏边| 沾化县| 彰化市| 景东| 称多县| 拉孜县| 阳城县| 隆昌县| 无为县| 龙胜| 玉门市| 河源市| 修武县| 南丰县| 曲沃县| 开远市| 深水埗区| 洛宁县| 漳浦县| 景德镇市|