贷款卡年审报告书,敏敏的阶下囚,郭天祥
本文实例讲述了python用于学习重要算法的模块pygorithm。分享给大家供大家参考,具体如下:
这是一个能够随时学习重要算法的python模块,纯粹是为了教学使用。
特点
安装
pip3 install pygorithm
*如果你使用的是python 2.7,请使用pip来安装。如果存在用户权限的限制,你可能需要使用
pip install --user pygorithm
这个命令来安装。
python setup.py install
快速入门
from pygorithm.sorting import bubble_sort mylist = [12, 4, 3, 5, 13, 1, 17, 19, 15] sortedlist = bubble_sort.sort(mylist) print(sortedlist)
运行结果:
[1, 3, 4, 5, 12, 13, 15, 17, 19]
from pygorithm.sorting import bubble_sort code = bubble_sort.get_code() print(code)
运行结果:
def sort(_list):
"""
bubble sorting algorithm:param _list: list of values to sort
:return: sorted values
"""
for i in range(len(_list)):
for j in range(len(_list) - 1, i, -1):
if _list[j] < _list[j - 1]:
_list[j], _list[j - 1] = _list[j - 1], _list[j]
return _list
from pygorithm.sorting import bubble_sort time_complexity = bubble_sort.time_complexities() print(time_complexity)
运行结果:
best case: o(n), average case: o(n ^ 2), worst case: o(n ^ 2).
for improved bubble sort:
best case: o(n); average case: o(n * (n - 1) / 4); worst case: o(n ^ 2)
>>> from pygorithm.sorting import modules >>> modules() ['bubble_sort', 'bucket_sort', 'counting_sort', 'heap_sort', 'insertion_sort', 'merge_sort', 'quick_sort', 'selection_sort', 'shell_sort']
测试
执行以下命令来运行所有的测试用例:
python3 -m unittest
这将运行tests/目录下的文件中定义的所有测试用例
更多关于python相关内容感兴趣的读者可查看本站专题:《python数据结构与算法教程》、《python编码操作技巧总结》、《python函数使用技巧总结》、《python字符串操作技巧汇总》及《python入门与进阶经典教程》
希望本文所述对大家python程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论