Python pygorithm模块用法示例【常见算法测试】


Posted in Python onAugust 16, 2018

本文实例讲述了Python pygorithm模块用法。分享给大家供大家参考,具体如下:

pygorithm:一个用纯粹python编写的Python模块,用于纯粹的教育目的。只需导入所需的算法即可获取代码,时间复杂度等等。开始学习Python编程的好方法。了解Python中所有主要算法的实现。不需要上网就可以获得所需的代码。

安装

pip3 install pygorithm

常见函数

斐波那契数列

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()   # 获取实现函数的算法
print(code)

获取最小公倍数

from pygorithm.math import lcm
result = lcm.lcm([4,6])
print(result)    # 12
code = lcm.get_code()      # 获取实现函数的算法
print(code)

质数算法

from pygorithm.math import sieve_of_eratosthenes
result = sieve_of_eratosthenes.sieve_of_eratosthenes(10)  # 获取小于10的质数
print(result)    # [2,3,5,7]
code = lcm.get_code()      # 获取实现函数的算法
print(code)

阶乘

from pygorithm.math import factorial
result = factorial.factorial(5)   # 获取5的阶乘,即1*2*3*4*5
print(result)    # 120
code = factorial.get_code()   # 获取实现函数的算法
print(code)

十进制转二进制

from pygorithm.math import conversion
result = conversion.decimal_to_binary(3)  # 将3转换为二进制
print(result)    # 11
code = conversion.get_code()  # 获取实现函数的算法
print(code)

二进制转十进制

from pygorithm.math import conversion
result = conversion.binary_to_decimal(11)  # 将11转换为十进制
print(result)    # 3
code = conversion.get_code()  # 获取实现函数的算法
print(code)

十进制转十六进制

from pygorithm.math import conversion
result = conversion.decimal_to_hex(15)   # 将15转换为十六进制数
print(result)    # F
code = conversion.get_code()  # 获取实现函数的算法
print(code)

十六进制转十进制

from pygorithm.math import conversion
result = conversion.hex_to_decimal("F")   # 将十六进制F转化为十进制数
print(result)    # 15
code = conversion.get_code()  # 获取实现函数的算法
print(code)

二分法搜索:效率高

from pygorithm.searching import binary_search
l = [9,4,5,1,7]
index = binary_search.search(l,5)   # 获取5在列表中的位置,找到返回下标,找不到返回False
print(index)
code = binary_search.get_code() # 获取实现函数的算法
print(code)

线性搜索:速度慢,适用性广

from pygorithm.searching import linear_search
l = [9,4,5,1,7]
index = linear_search.search(l,5)    # 获取5在列表中的位置,找到返回下标,找不到返回False
print(index)
code = linear_search.get_code() # 获取实现函数的算法
print(code)

插值搜索:注意:列表必须先经过升序排序,否则将找不到

from pygorithm.searching import interpolation_search
l = [1,4,5,7,9]
index = interpolation_search.search(l,4)  # 获取5在列表中的位置,找到返回下标,找不到返回False
print(index)
code = interpolation.get_code() # 获取实现函数的算法
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()  # 获取实现函数的算法
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()  # 获取实现函数的算法
print(code)

计数排序

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() # 获取实现函数的算法
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()   # 获取实现函数的算法
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()  # 获取实现函数的算法
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()    # 获取实现函数的算法
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()    # 获取实现函数的算法
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()  # 获取实现函数的算法
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()    # 获取实现函数的算法
print(code)

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

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python常用模块用法分析
Sep 08 Python
python冒泡排序简单实现方法
Jul 09 Python
python的pdb调试命令的命令整理及实例
Jul 12 Python
python去掉空白行的多种实现代码
Mar 19 Python
pytorch中tensor的合并与截取方法
Jul 26 Python
Python Socket编程之多线程聊天室
Jul 28 Python
PyQt 实现使窗口中的元素跟随窗口大小的变化而变化
Jun 18 Python
django框架基于模板 生成 excel(xls) 文件操作示例
Jun 19 Python
Tensorflow分批量读取数据教程
Feb 07 Python
Python实现猜年龄游戏代码实例
Mar 25 Python
解决pymysql cursor.fetchall() 获取不到数据的问题
May 15 Python
TensorFlow中tf.batch_matmul()的用法
Jun 02 Python
Python使用pickle模块报错EOFError Ran out of input的解决方法
Aug 16 #Python
Python使用pickle模块储存对象操作示例
Aug 15 #Python
Linux下多个Python版本安装教程
Aug 15 #Python
Python并发之多进程的方法实例代码
Aug 15 #Python
Python使用sort和class实现的多级排序功能示例
Aug 15 #Python
Python常见排序操作示例【字典、列表、指定元素等】
Aug 15 #Python
Centos下实现安装Python3.6和Python2共存
Aug 15 #Python
You might like
php开发过程中关于继承的使用方法分享
2011/06/17 PHP
在Windows系统下使用PHP生成Word文档的教程
2015/07/03 PHP
PHP使用PDO操作sqlite数据库应用案例
2019/03/07 PHP
PHP中md5()函数的用法讲解
2019/03/30 PHP
jQuery结合Json提交数据到Webservice,并接收从Webservice返回的Json数据
2011/02/18 Javascript
JavaScript 开发工具webstrom使用指南
2014/12/09 Javascript
JavaScript DOM 学习总结(五)
2015/11/24 Javascript
JavaScript中的return语句简单介绍
2015/12/07 Javascript
jQuery实现的分子运动小球碰撞效果
2016/01/27 Javascript
JavaScript中的原型prototype完全解析
2016/05/10 Javascript
Jquery和BigFileUpload实现大文件上传及进度条显示
2016/06/27 Javascript
easyui关于validatebox实现多重规则验证的方法(必看)
2017/04/12 Javascript
详解angularjs 学习之 scope作用域
2018/01/15 Javascript
vue使用swiper.js重叠轮播组建样式
2019/11/14 Javascript
解决antd日期选择组件,添加value就无法点击下一年和下一月问题
2020/10/29 Javascript
[01:12:53]完美世界DOTA2联赛PWL S2 Forest vs SZ 第一场 11.25
2020/11/26 DOTA
Python切换pip安装源的方法详解
2016/11/18 Python
利用Python生成文件md5校验值函数的方法
2017/01/10 Python
Python用for循环实现九九乘法表
2018/05/31 Python
Python3 获取一大段文本之间两个关键字之间的内容方法
2018/10/11 Python
Python爬虫动态ip代理防止被封的方法
2019/07/07 Python
Python爬虫 scrapy框架爬取某招聘网存入mongodb解析
2019/07/31 Python
基于python操作ES实例详解
2019/11/16 Python
Python识别html主要文本框过程解析
2020/02/18 Python
sklearn线性逻辑回归和非线性逻辑回归的实现
2020/06/09 Python
Python爬虫实现HTTP网络请求多种实现方式
2020/06/19 Python
css3高级选择器使用方法
2013/12/02 HTML / CSS
CSS3实现时间轴特效
2020/11/02 HTML / CSS
html5之Canvas路径绘图、坐标变换应用实例
2012/12/26 HTML / CSS
哄娃神器4moms商店:美国婴童用品品牌
2019/03/07 全球购物
运动会通讯稿300字
2014/02/02 职场文书
分居协议书范本(律师见证版)
2014/11/26 职场文书
个人工作年终总结
2015/03/09 职场文书
2015年圣诞节寄语
2015/08/17 职场文书
PHP实现考试倒计时功能代码
2021/04/16 PHP
GoFrame基于性能测试得知grpool使用场景
2022/06/21 Golang