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斗牛游戏的概率
Feb 12 Python
python数据清洗系列之字符串处理详解
Feb 12 Python
ubuntu安装sublime3并配置python3环境的方法
Mar 15 Python
深入浅析Python的类
Jun 22 Python
python 遍历列表提取下标和值的实例
Dec 25 Python
详细介绍pandas的DataFrame的append方法使用
Jul 31 Python
Python制作词云图代码实例
Sep 09 Python
python实现按首字母分类查找功能
Oct 31 Python
python飞机大战pygame游戏框架搭建操作详解
Dec 17 Python
Python编程快速上手——正则表达式查找功能案例分析
Feb 28 Python
Python实现生活常识解答机器人
Jun 28 Python
Python机器学习应用之基于线性判别模型的分类篇详解
Jan 18 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
第十四节 命名空间 [14]
2006/10/09 PHP
PHP Google的translate API代码
2008/12/10 PHP
CI框架整合smarty步骤详解
2016/05/19 PHP
PHP编程实现微信企业向用户付款的方法示例
2017/07/26 PHP
关于 Laravel Redis 多个进程同时取队列问题详解
2017/12/25 PHP
php设计模式之适配器模式实例分析【星际争霸游戏案例】
2020/04/07 PHP
jQuery实现的类flash菜单效果代码
2010/05/17 Javascript
Javascript中获取出错代码所在文件及行数的代码
2010/09/23 Javascript
JS+ACTIVEX实现网页选择本地目录路径对话框
2013/03/18 Javascript
鼠标拖动实现DIV排序示例代码
2013/10/14 Javascript
我的Node.js学习之路(三)--node.js作用、回调、同步和异步代码 以及事件循环
2014/07/06 Javascript
jquery和js实现对div的隐藏和显示方法
2014/09/26 Javascript
JS FormData上传文件的设置方法
2017/07/05 Javascript
Mac下安装vue
2018/04/11 Javascript
LayUi数据表格自定义赋值方式
2019/10/26 Javascript
[01:43]倾听DOTA2英雄之声 魅惑魔女国服配音鉴赏
2013/06/06 DOTA
[01:14:55]EG vs Spirit Supermajor 败者组 BO3 第三场 6.4
2018/06/05 DOTA
深入浅析Python中list的复制及深拷贝与浅拷贝
2018/09/03 Python
python利用小波分析进行特征提取的实例
2019/01/09 Python
在windows下使用python进行串口通讯的方法
2019/07/02 Python
Python 解决火狐浏览器不弹出下载框直接下载的问题
2020/03/09 Python
python 两种方法删除空文件夹
2020/09/29 Python
I.T集团香港官方商城:ITeSHOP.com Hong Kong
2019/02/15 全球购物
英国时尚配饰、珠宝和服装网站:KJ Beckett
2020/01/23 全球购物
SQL Server面试题
2016/10/17 面试题
南京某软件公司的.net面试题
2015/11/30 面试题
什么是虚拟内存?虚拟内存有什么优势?
2012/02/19 面试题
会计专业求职信范文
2014/03/16 职场文书
《大自然的语言》教学反思
2014/04/08 职场文书
教师节晚会主持词
2015/06/30 职场文书
加强党性修养心得体会
2016/01/21 职场文书
Python中OpenCV实现查找轮廓的实例
2021/06/08 Python
MySQL如何解决幻读问题
2021/08/07 MySQL
使用CSS实现一个搜索引擎的原理解析
2021/09/25 HTML / CSS
Go gRPC进阶教程gRPC转换HTTP
2022/06/16 Golang
Sentry的安装、配置、使用教程(Sentry日志手机系统)
2022/07/23 Python