Algorithms,最全的Python算法仓库

开发 前端
Python的练手项目很多,特别是Github上,建议不管新手、老司机都去看看。这里推荐给大家一个Gitthub上练习的项目,算法仓库-algorithms。

​学习编程、学习Python最好的方式就是练习,哪怕是新手,只要不断地敲代码输出,肯定会有神效。

Python的练手项目很多,特别是Github上,建议不管新手、老司机都去看看。

Algorithms,最全的Python算法仓库

这里推荐给大家一个Gitthub上练习的项目,算法仓库-algorithms。

https://github.com/keon/algorithms

这里面集合众多核心算法的Python实现,比如排序、图计算、回溯、队列、流计算、堆、搜索、压缩等等。

Algorithms,最全的Python算法仓库

该仓库支持第三方库安装,在python中进行调用,非常方便。

首先使用pip进行安装:

pip3 install algorithms

然后导入相关模块进行调用,比如sort模块里的merge_sort归并排序算法。

from algorithms.sort import merge_sort

if __name__ == "__main__":
my_list = [1, 8, 3, 5, 6]
my_list = merge_sort(my_list)
print(my_list)

举几个常见的算法案例。

1. 排序算法-桶排序

def bucket_sort(arr):
''' Bucket Sort
Complexity: O(n^2)
The complexity is dominated by nextSort
'''
# The number of buckets and make buckets
num_buckets = len(arr)
buckets = [[] for bucket in range(num_buckets)]
# Assign values into bucket_sort
for value in arr:
index = value * num_buckets // (max(arr) + 1)
buckets[index].append(value)
# Sort
sorted_list = []
for i in range(num_buckets):
sorted_list.extend(next_sort(buckets[i]))
return sorted_list

def next_sort(arr):
# We will use insertion sort here.
for i in range(1, len(arr)):
j = i - 1
key = arr[i]
while arr[j] > key and j >= 0:
arr[j+1] = arr[j]
j = j - 1
arr[j + 1] = key
return arr

2. 机器学习-最近邻插值法

import math

def distance(x,y):
"""[summary]
HELPER-FUNCTION
calculates the (eulidean) distance between vector x and y.

Arguments:
x {[tuple]} -- [vector]
y {[tuple]} -- [vector]
"""
assert len(x) == len(y), "The vector must have same length"
result = ()
sum = 0
for i in range(len(x)):
result += (x[i] -y[i],)
for component in result:
sum += component**2
return math.sqrt(sum)

def nearest_neighbor(x, tSet):
"""[summary]
Implements the nearest neighbor algorithm

Arguments:
x {[tupel]} -- [vector]
tSet {[dict]} -- [training set]

Returns:
[type] -- [result of the AND-function]
"""
assert isinstance(x, tuple) and isinstance(tSet, dict)
current_key = ()
min_d = float('inf')
for key in tSet:
d = distance(x, key)
if d < min_d:
min_d = d
current_key = key
return tSet[current_key]

3. 字符串解码编码

# Implement the encode and decode methods.

def encode(strs):
"""Encodes a list of strings to a single string.
:type strs: List[str]
:rtype: str
"""
res = ''
for string in strs.split():
res += str(len(string)) + ":" + string
return res

def decode(s):
"""Decodes a single string to a list of strings.
:type s: str
:rtype: List[str]
"""
strs = []
i = 0
while i < len(s):
index = s.find(":", i)
size = int(s[i:index])
strs.append(s[index+1: index+1+size])
i = index+1+size
return strs

4. 直方分布

def get_histogram(input_list: list) -> dict:
"""
Get histogram representation
:param input_list: list with different and unordered values
:return histogram: dict with histogram of input_list
"""
# Create dict to store histogram
histogram = {}
# For each list value, add one to the respective histogram dict position
for i in input_list:
histogram[i] = histogram.get(i, 0) + 1
return histogram

个人感觉这个仓库里的算法很齐全,适合做练习,小伙伴们可以试试。​

责任编辑:赵宁宁 来源: 今日头条
相关推荐

2018-06-04 16:35:22

python编程语言人工智能

2024-03-22 15:32:21

机器学习算法

2024-03-01 08:07:31

*和**Python符号

2017-09-15 15:48:12

Python面向对象技术类与对象

2020-09-17 11:08:53

GitHubPython仓库

2011-04-13 10:48:33

算法程序员

2022-04-13 18:40:59

Python开发

2011-03-22 15:48:40

LAMP

2023-03-11 22:22:04

数据库MySQL

2023-03-03 09:09:55

2019-10-29 15:09:52

Python贪心算法代码

2022-08-31 08:54:57

AIDALL-E 2OpenAI

2023-12-15 10:27:01

暴力匹配算法Python字符串

2015-12-01 09:56:49

数据中心机房

2011-02-22 14:40:25

vsftpd

2009-07-25 16:52:29

2011-02-22 14:21:49

vsftpd

2019-12-23 15:23:12

人工智能机器学习技术

2024-04-26 00:02:00

云技术公有云私有云

2019-11-18 12:41:35

算法Python计算复杂性理论
点赞
收藏

51CTO技术栈公众号