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 相关文章推荐
2款Python内存检测工具介绍和使用方法
Jun 01 Python
python自动化测试之setUp与tearDown实例
Sep 28 Python
Python中tell()方法的使用详解
May 24 Python
Python通过调用mysql存储过程实现更新数据功能示例
Apr 03 Python
对Python3+gdal 读取tiff格式数据的实例讲解
Dec 04 Python
python mac下安装虚拟环境的图文教程
Apr 12 Python
Python实用工具FuckIt.py介绍
Jul 02 Python
获取Pytorch中间某一层权重或者特征的例子
Aug 17 Python
Django中的cookie和session
Aug 27 Python
python如何从键盘获取输入实例
Jun 18 Python
conda安装tensorflow和conda常用命令小结
Feb 20 Python
使用pandas读取表格数据并进行单行数据拼接的详细教程
Mar 03 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
php5.2时间相差8小时
2007/01/15 PHP
Laravel 5框架学习之Eloquent 关系
2015/04/09 PHP
PHP上传Excel文件导入数据到MySQL数据库示例
2016/10/25 PHP
Thinkphp5结合layer弹窗定制操作结果页面
2017/07/07 PHP
PHP Socket网络操作类定义与用法示例
2017/08/30 PHP
CI框架(CodeIgniter)操作redis的方法详解
2018/01/25 PHP
jquery创建一个ajax关键词数据搜索实现思路
2013/02/26 Javascript
javascript实现文字图片上下滚动的具体实例
2013/06/28 Javascript
JavaScript计算某一天是星期几的方法
2015/08/05 Javascript
JavaScript面向对象编写购物车功能
2016/08/19 Javascript
微信js-sdk界面操作接口用法示例
2016/10/12 Javascript
Angularjs之filter过滤器(推荐)
2016/11/27 Javascript
JavaScript实现实时更新系统时间的实例代码
2017/04/04 Javascript
jQuery实现全选、反选和不选功能
2017/08/16 jQuery
vue页面离开后执行函数的实例
2018/03/13 Javascript
红黑树的插入详解及Javascript实现方法示例
2018/03/26 Javascript
浅谈Vue数据响应思路之数组
2018/11/06 Javascript
Vue中Table组件Select的勾选和取消勾选事件详解
2019/03/19 Javascript
在Vue中使用antv的示例代码
2020/06/29 Javascript
详解Vue之事件处理
2020/07/10 Javascript
python通过scapy获取局域网所有主机mac地址示例
2014/05/04 Python
python服务器端收发请求的实现代码
2014/09/29 Python
Python cookbook(字符串与文本)针对任意多的分隔符拆分字符串操作示例
2018/04/19 Python
从请求到响应过程中django都做了哪些处理
2018/08/01 Python
Python中整数的缓存机制讲解
2019/02/16 Python
Python列表与元组的异同详解
2019/07/02 Python
解决pytorch报错:AssertionError: Invalid device id的问题
2020/01/10 Python
css3的transform造成z-index无效解决方案
2014/12/04 HTML / CSS
HTML5 对各个标签的定义与规定:body的介绍
2012/06/21 HTML / CSS
HTML5中的拖放实现详解
2017/08/23 HTML / CSS
自动化专业本科毕业生求职信
2013/10/20 职场文书
《狼和小羊》教学反思
2014/04/20 职场文书
财务工作犯错检讨书
2014/10/07 职场文书
销售经理工作失职检讨书
2014/10/24 职场文书
放弃遗产继承公证书
2015/01/26 职场文书
详解nginx location指令
2022/01/18 Servers