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两种遍历字典(dict)的方法比较
May 29 Python
python在windows下实现备份程序实例
Jul 04 Python
python实现给数组按片赋值的方法
Jul 28 Python
Python 3实战爬虫之爬取京东图书的图片详解
Oct 09 Python
详细分析python3的reduce函数
Dec 05 Python
对python遍历文件夹中的所有jpg文件的实例详解
Dec 08 Python
Python中numpy模块常见用法demo实例小结
Mar 16 Python
Python Tornado批量上传图片并显示功能
Mar 26 Python
python针对Oracle常见查询操作实例分析
Apr 30 Python
在Django中自定义filter并在template中的使用详解
May 19 Python
Python中time标准库的使用教程
Apr 13 Python
Python中的socket网络模块介绍
Jul 23 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的引用计数机制
2013/06/14 PHP
php有效防止图片盗用、盗链的两种方法
2016/11/01 PHP
thinkphp5 URL和路由的功能详解与实例
2017/12/26 PHP
jquery插件制作 自增长输入框实现代码
2012/08/17 jQuery
jquery统计复选框选中示例
2013/11/05 Javascript
JS批量操作CSS属性详细解析
2013/12/16 Javascript
使用JavaScript脚本无法直接改变Asp.net中Checkbox控件的Enable属性的解决方法
2015/09/16 Javascript
JavaScript中eval()函数用法详解
2015/12/14 Javascript
JS实现拖动滚动条评分的效果代码分享
2016/09/29 Javascript
JS中的多态实例详解
2017/10/15 Javascript
jQuery访问浏览器本地存储cookie、localStorage和sessionStorage的基本用法
2017/10/20 jQuery
vue select二级联动第二级默认选中第一个option值的实例
2018/01/10 Javascript
详解AngularJS之$window窗口对象
2018/01/17 Javascript
解决layui数据表格Date日期格式的回显Object的问题
2019/09/19 Javascript
viewer.js一个强大的基于jQuery的图像查看插件(支持旋转、缩放)
2020/04/01 jQuery
微信小程序仿抖音短视频切换效果的实例代码
2020/06/24 Javascript
Vue切换Tab动态渲染组件的操作
2020/09/21 Javascript
[56:18]VGJ.S vs Secret 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python中查看变量内存地址的方法
2015/05/05 Python
简单上手Python中装饰器的使用
2015/07/12 Python
在Python的Django框架中显示对象子集的方法
2015/07/21 Python
python实现决策树分类算法
2017/12/21 Python
python生成ppt的方法
2018/06/07 Python
浅谈Django+Gunicorn+Nginx部署之路
2019/09/11 Python
Python上下文管理器类和上下文管理器装饰器contextmanager用法实例分析
2019/11/07 Python
PyTorch在Windows环境搭建的方法步骤
2020/05/12 Python
tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)
2020/06/30 Python
Python如何输出百分比
2020/07/31 Python
Django框架实现在线考试系统的示例代码
2020/11/30 Python
详解移动端h5页面根据屏幕适配的四种方案
2020/04/15 HTML / CSS
意大利男装网店:Vrients
2019/05/02 全球购物
学生打架检讨书1000字
2014/01/16 职场文书
企业安全生产承诺书
2014/05/22 职场文书
创业计划书之网吧
2019/10/10 职场文书
react中的DOM操作实现
2021/06/30 Javascript
Pygame Event事件模块的详细示例
2021/11/17 Python