Python pygorithm模塊用法示例【常見算法測試】
本文實例講述了Python pygorithm模塊用法。分享給大家供大家參考,具體如下:
pygorithm:一個用純粹python編寫的Python模塊,用于純粹的教育目的。只需導(dǎo)入所需的算法即可獲取代碼,時間復(fù)雜度等等。開始學(xué)習(xí)Python編程的好方法。了解Python中所有主要算法的實現(xiàn)。不需要上網(wǎng)就可以獲得所需的代碼。
安裝
pip3 install pygorithm
常見函數(shù)
斐波那契數(shù)列
from pygorithm.fibonacci import recursion result = recursion.get_sequence(10) print(result) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] code = recursion.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
獲取最小公倍數(shù)
from pygorithm.math import lcm result = lcm.lcm([4,6]) print(result) # 12 code = lcm.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
質(zhì)數(shù)算法
from pygorithm.math import sieve_of_eratosthenes result = sieve_of_eratosthenes.sieve_of_eratosthenes(10) # 獲取小于10的質(zhì)數(shù) print(result) # [2,3,5,7] code = lcm.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
階乘
from pygorithm.math import factorial result = factorial.factorial(5) # 獲取5的階乘,即1*2*3*4*5 print(result) # 120 code = factorial.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
十進制轉(zhuǎn)二進制
from pygorithm.math import conversion result = conversion.decimal_to_binary(3) # 將3轉(zhuǎn)換為二進制 print(result) # 11 code = conversion.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
二進制轉(zhuǎn)十進制
from pygorithm.math import conversion result = conversion.binary_to_decimal(11) # 將11轉(zhuǎn)換為十進制 print(result) # 3 code = conversion.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
十進制轉(zhuǎn)十六進制
from pygorithm.math import conversion result = conversion.decimal_to_hex(15) # 將15轉(zhuǎn)換為十六進制數(shù) print(result) # F code = conversion.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
十六進制轉(zhuǎn)十進制
from pygorithm.math import conversion result = conversion.hex_to_decimal("F") # 將十六進制F轉(zhuǎn)化為十進制數(shù) print(result) # 15 code = conversion.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
二分法搜索:效率高
from pygorithm.searching import binary_search l = [9,4,5,1,7] index = binary_search.search(l,5) # 獲取5在列表中的位置,找到返回下標(biāo),找不到返回False print(index) code = binary_search.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
線性搜索:速度慢,適用性廣
from pygorithm.searching import linear_search l = [9,4,5,1,7] index = linear_search.search(l,5) # 獲取5在列表中的位置,找到返回下標(biāo),找不到返回False print(index) code = linear_search.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
插值搜索:注意:列表必須先經(jīng)過升序排序,否則將找不到
from pygorithm.searching import interpolation_search l = [1,4,5,7,9] index = interpolation_search.search(l,4) # 獲取5在列表中的位置,找到返回下標(biāo),找不到返回False print(index) code = interpolation.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
冒泡排序
from pygorithm.sorting import bubble_sort l = [9,4,5,1,7] result = bubble_sort.sort(l) print(result) # [1, 4, 5, 7, 9] code = bubble_sort.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
改良冒泡排序
from pygorithm.sorting import bubble_sort l = [9,4,5,1,7] result = bubble_sort.improved_sort(l) print(result) # [1, 4, 5, 7, 9]
桶排序
from pygorithm.sorting import bucket_sort l = [9,4,5,1,7] result = bucket_sort.sort(l,5) # 5為桶的大小,默認為5 print(result) # [1, 4, 5, 7, 9] code = bucket_sort.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
計數(shù)排序
from pygorithm.sorting import counting_sort l = [9,4,5,1,7] result = counting_sort.sort(l) print(result) # [1, 4, 5, 7, 9] code = counting_sort.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
堆排序
from pygorithm.sorting import heap_sort l = [9,4,5,1,7] result = heap_sort.sort(l) print(result) # [1, 4, 5, 7, 9] code = heap_sort.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
插入排序
from pygorithm.sorting import insertion_sort l = [9,4,5,1,7] result = insertion_sort(l) print(result) # [1, 4, 5, 7, 9] code = insertion_sort.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
歸并排序
from pygorithm.sorting import merge_sort l = [9,4,5,1,7] result = merge_sort.sort(l) print(result) # [1, 4, 5, 7, 9] code = merge_sort.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
快速排序
from pygorithm.sorting import quick_sort l = [9,4,5,1,7] result = quick_sort.sort(l) print(result) # [1, 4, 5, 7, 9] code = quick_sort.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
選擇排序
from pygorithm.sorting import selection_sort l = [9,4,5,1,7] result = selection_sort.sort(l) print(result) # [1, 4, 5, 7, 9] code = selection_sort.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
希爾排序
from pygorithm.sorting import shell_sort l = [9,4,5,1,7] result = shell_sort.sort(l) print(result) # [1, 4, 5, 7, 9] code = shell_sort.get_code() # 獲取實現(xiàn)函數(shù)的算法 print(code)
更多經(jīng)典算法: http://pygorithm.readthedocs.io/en/latest/index.html
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
用pytorch的nn.Module構(gòu)造簡單全鏈接層實例
今天小編就為大家分享一篇用pytorch的nn.Module構(gòu)造簡單全鏈接層實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python操作MySQL數(shù)據(jù)庫的方法分享
堅持每天學(xué)一點,每天積累一點點,作為自己每天的業(yè)余收獲,這個文章是我在吃飯的期間寫的,利用自己零散的時間學(xué)了一下python操作MYSQL,所以整理一下2012-05-05Python 動態(tài)變量名定義與調(diào)用方法
這篇文章主要介紹了Python 動態(tài)變量名定義與調(diào)用方法,需要的朋友可以參考下2020-02-02使用Python實現(xiàn)下載網(wǎng)易云音樂的高清MV
本文給大家分享的是一則使用Python實現(xiàn)下載網(wǎng)易云音樂中高清MV的代碼,本人新手,沒有做特別的功能,僅僅是直接循環(huán)了MV的id,小伙伴們可以自己擴展下。2015-03-03關(guān)于python的xlwings與VBA間的互相調(diào)用
這篇文章主要介紹了關(guān)于python的xlwings與VBA間的互相調(diào)用,VBA是一種通用應(yīng)用軟件腳本語言,Excel包含和許多功能強大的數(shù)據(jù)分析對象,例如工作表、圖表、數(shù)據(jù)透視表以及大量的數(shù)學(xué)、財務(wù)、工程和通用業(yè)務(wù)函數(shù),配合VBA可以運用這些對象開發(fā)出自動程序2023-07-07