Python pygorithm模塊用法示例【常見算法測(cè)試】
本文實(shí)例講述了Python pygorithm模塊用法。分享給大家供大家參考,具體如下:
pygorithm:一個(gè)用純粹python編寫的Python模塊,用于純粹的教育目的。只需導(dǎo)入所需的算法即可獲取代碼,時(shí)間復(fù)雜度等等。開始學(xué)習(xí)Python編程的好方法。了解Python中所有主要算法的實(shí)現(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() # 獲取實(shí)現(xiàn)函數(shù)的算法 print(code)
獲取最小公倍數(shù)
from pygorithm.math import lcm result = lcm.lcm([4,6]) print(result) # 12 code = lcm.get_code() # 獲取實(shí)現(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() # 獲取實(shí)現(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() # 獲取實(shí)現(xiàn)函數(shù)的算法 print(code)
十進(jìn)制轉(zhuǎn)二進(jìn)制
from pygorithm.math import conversion result = conversion.decimal_to_binary(3) # 將3轉(zhuǎn)換為二進(jìn)制 print(result) # 11 code = conversion.get_code() # 獲取實(shí)現(xiàn)函數(shù)的算法 print(code)
二進(jìn)制轉(zhuǎn)十進(jìn)制
from pygorithm.math import conversion result = conversion.binary_to_decimal(11) # 將11轉(zhuǎn)換為十進(jìn)制 print(result) # 3 code = conversion.get_code() # 獲取實(shí)現(xiàn)函數(shù)的算法 print(code)
十進(jìn)制轉(zhuǎn)十六進(jìn)制
from pygorithm.math import conversion result = conversion.decimal_to_hex(15) # 將15轉(zhuǎn)換為十六進(jìn)制數(shù) print(result) # F code = conversion.get_code() # 獲取實(shí)現(xiàn)函數(shù)的算法 print(code)
十六進(jìn)制轉(zhuǎn)十進(jìn)制
from pygorithm.math import conversion
result = conversion.hex_to_decimal("F") # 將十六進(jìn)制F轉(zhuǎn)化為十進(jìn)制數(shù)
print(result) # 15
code = conversion.get_code() # 獲取實(shí)現(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() # 獲取實(shí)現(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() # 獲取實(shí)現(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() # 獲取實(shí)現(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() # 獲取實(shí)現(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為桶的大小,默認(rèn)為5 print(result) # [1, 4, 5, 7, 9] code = bucket_sort.get_code() # 獲取實(shí)現(xiàn)函數(shù)的算法 print(code)
計(jì)數(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() # 獲取實(shí)現(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() # 獲取實(shí)現(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() # 獲取實(shí)現(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() # 獲取實(shí)現(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() # 獲取實(shí)現(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() # 獲取實(shí)現(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() # 獲取實(shí)現(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ìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python算法之棧(stack)的實(shí)現(xiàn)
- Python實(shí)現(xiàn)的Kmeans++算法實(shí)例
- python使用rsa加密算法模塊模擬新浪微博登錄
- 樸素貝葉斯算法的python實(shí)現(xiàn)方法
- python冒泡排序算法的實(shí)現(xiàn)代碼
- python編寫的最短路徑算法
- kNN算法python實(shí)現(xiàn)和簡(jiǎn)單數(shù)字識(shí)別的方法
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之鏈表詳解
- python 算法 排序?qū)崿F(xiàn)快速排序
- Python用于學(xué)習(xí)重要算法的模塊pygorithm實(shí)例淺析
相關(guān)文章
Python學(xué)習(xí)之Django的管理界面代碼示例
這篇文章主要介紹了Python學(xué)習(xí)之Django的管理界面代碼示例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Python UI自動(dòng)化測(cè)試Web frame及多窗口切換
這篇文章主要為大家介紹了Python UI自動(dòng)化測(cè)試Web frame及多窗口切換,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
pandas進(jìn)行時(shí)間數(shù)據(jù)的轉(zhuǎn)換和計(jì)算時(shí)間差并提取年月日
這篇文章主要介紹了pandas進(jìn)行時(shí)間數(shù)據(jù)的轉(zhuǎn)換和計(jì)算時(shí)間差并提取年月日,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python實(shí)現(xiàn)Linux命令xxd -i功能
這篇文章主要介紹了Python實(shí)現(xiàn)Linux命令xxd -i功能的相關(guān)資料,需要的朋友可以參考下2016-03-03
10行Python代碼計(jì)算汽車數(shù)量的實(shí)現(xiàn)方法
這篇文章主要介紹了10行Python代碼計(jì)算汽車數(shù)量的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python和perl實(shí)現(xiàn)批量對(duì)目錄下電子書文件重命名的代碼分享
這篇文章主要介紹了Python和perl實(shí)現(xiàn)批量對(duì)目錄下電子書文件重命名的代碼分享,本文同時(shí)給出了Python和perl兩種語言的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-11-11
正則給header的冒號(hào)兩邊參數(shù)添加單引號(hào)(Python請(qǐng)求用)
這篇文章主要介紹了正則給header的冒號(hào)兩邊參數(shù)添加單引號(hào)(Python請(qǐng)求用)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
python使用in操作符時(shí)元組和數(shù)組的區(qū)別分析
有時(shí)候要判斷一個(gè)數(shù)是否在一個(gè)序列里面,這時(shí)就會(huì)用到in運(yùn)算符來判斷成員資格,如果條件為真時(shí),就會(huì)返回true,條件為假時(shí),返回一個(gè)flase。這樣的運(yùn)算符叫做布爾運(yùn)算符,其真值叫做布爾值。2015-05-05

