成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

個人感覺這個倉庫里的算法很齊全,適合做練習,小伙伴們可以試試。?

責任編輯:趙寧寧 來源: 今日頭條
相關推薦

2025-05-08 01:00:00

Nginx算法負載均衡

2024-03-22 15:32:21

機器學習算法

2018-06-04 16:35:22

python編程語言人工智能

2024-03-01 08:07:31

*和**Python符號

2017-09-15 15:48:12

Python面向對象技術類與對象

2011-04-13 10:48:33

算法程序員

2011-03-22 15:48:40

LAMP

2023-03-11 22:22:04

數據庫MySQL

2011-02-22 14:40:25

vsftpd

2015-12-01 09:56:49

數據中心機房

2011-02-22 14:21:49

vsftpd

2009-07-25 16:52:29

2022-04-13 18:40:59

Python開發

2020-09-17 11:08:53

GitHubPython倉庫

2024-05-27 00:04:00

2024-04-26 00:02:00

云技術公有云私有云

2024-06-13 10:16:29

2022-08-31 08:54:57

AIDALL-E 2OpenAI

2024-01-07 13:33:14

2017-12-25 09:59:14

服務器硬盤接口
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久精品一 | 日韩一区二区免费视频 | 免费国产黄 | 成人二区 | www操操| 国产激情在线观看视频 | 一区二区三区欧美 | 91视频在线看 | 成人网在线| 国产精品亚洲第一区在线暖暖韩国 | 午夜激情小视频 | 精品一区二区在线观看 | 国内精品久久精品 | 欧美成人猛片aaaaaaa | 国产精品一二区 | 精品久久久久久久久久久 | 国产精品久久久久久婷婷天堂 | 国产色播av在线 | 精品国产乱码久久久久久1区2区 | 国产免费一区二区三区网站免费 | 日韩一区二区三区在线观看 | 亚洲欧美中文日韩在线v日本 | 婷婷丁香激情 | 精品久久香蕉国产线看观看亚洲 | 美女天天干天天操 | 成人亚洲性情网站www在线观看 | 欧美jizzhd精品欧美巨大免费 | 色性av | 日韩免费看视频 | 国产一区精品在线 | 亚洲精品久久久久久宅男 | 国产精品综合网 | 久久精品国产一区二区电影 | 久久久精品一区 | 国产电影一区二区 | 91麻豆精品国产91久久久更新资源速度超快 | 国产成人精品区一区二区不卡 | 亚洲精品一区二区二区 | 国产网站在线免费观看 | 在线观看三级av | 四虎成人免费视频 |