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下载懒人图库JavaScript特效
May 28 Python
详解Python操作RabbitMQ服务器消息队列的远程结果返回
Jun 30 Python
Python中字典和集合学习小结
Jul 07 Python
Python 快速实现CLI 应用程序的脚手架
Dec 05 Python
python3.5 tkinter实现页面跳转
Jan 30 Python
django限制匿名用户访问及重定向的方法实例
Feb 07 Python
django做form表单的数据验证过程详解
Jul 26 Python
处理Selenium3+python3定位鼠标悬停才显示的元素
Jul 31 Python
python集合能干吗
Jul 19 Python
python输入中文的实例方法
Sep 14 Python
python自动化测试三部曲之unittest框架的实现
Oct 07 Python
五分钟学会怎么用Pygame做一个简单的贪吃蛇
Jan 06 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 远程图片保存到本地的函数类
2008/12/08 PHP
深入for,while,foreach遍历时间比较的详解
2013/06/08 PHP
php中的mongodb select常用操作代码示例
2014/09/06 PHP
正确的PHP匹配UTF-8中文的正则表达式
2015/05/13 PHP
Yii框架表单提交验证功能分析
2017/01/07 PHP
php+Ajax处理xml与json格式数据的方法示例
2019/03/04 PHP
Thinkphp集成抖音SDK的实现方法
2020/04/28 PHP
网页中实现浏览器的最大,最小化和关闭按钮
2007/03/12 Javascript
jquery ajax 同步异步的执行示例代码
2010/06/23 Javascript
基于jquery的网站幻灯片切换效果焦点图代码
2013/09/15 Javascript
jQuery实现的感应鼠标悬停图片色彩渐显效果
2015/03/03 Javascript
arguments对象验证函数的参数是否合法
2015/06/26 Javascript
JS模式之单例模式基本用法
2015/06/30 Javascript
jquery+php随机生成红包金额数量代码分享
2015/08/27 Javascript
JS实现来回出现文字的状态栏特效代码
2015/10/31 Javascript
Vue项目中quill-editor带样式编辑器的使用方法
2017/08/08 Javascript
详解Angular cli配置过程记录
2019/11/07 Javascript
[02:08]什么藏在DOTA2 TI9“小紫本”里?斧王历险记告诉你!
2019/05/17 DOTA
python计算N天之后日期的方法
2015/03/31 Python
各种Python库安装包下载地址与安装过程详细介绍(Windows版)
2016/11/02 Python
python3+PyQt5+Qt Designer实现扩展对话框
2018/04/20 Python
对python字典元素的添加与修改方法详解
2018/07/06 Python
Sanic框架流式传输操作示例
2018/07/18 Python
详解Python中is和==的区别
2019/03/21 Python
python字典嵌套字典的情况下找到某个key的value详解
2019/07/10 Python
Python 根据日志级别打印不同颜色的日志的方法示例
2019/08/08 Python
Python 类的私有属性和私有方法实例分析
2019/09/29 Python
国际化的太阳镜及太阳镜配件零售商:Sunglass Hut
2016/07/26 全球购物
前台文员岗位职责
2013/12/28 职场文书
入党积极分子思想汇报范文
2014/01/05 职场文书
新郎婚宴答谢词
2014/01/19 职场文书
安全检查管理制度
2014/02/02 职场文书
课外活动总结范文
2014/07/09 职场文书
商标侵权律师函
2015/05/27 职场文书
PHP策略模式写法
2021/04/01 PHP
JavaScript阻止事件冒泡的方法
2021/12/06 Javascript