github上最大的开源算法库,可以用于算法的学习和查询,大部分语言都有实现方案,其中python相关达到了165k star。
1 TheAlgorithms/Python简介
所有算法都用python实现的案例展示,常规的算法都可以在这里找到,应该是github上最全的开源算法库了。
什么是算法?
算法是一系列规则,这些规则通过获得一个或者多个输入,在内部进行计算、进行数据处理后,产生一个或者多个输出。简单地说,算法让生活更加美好。从复杂的数据处理、散列,到简单的数学运算,算法遵从一系列步骤来产出一个有用的结果。一个最简单的算法就是一个接受两个输入,把他们相加,然后输出他们的和的函数。
2 如何查看?
github可以访问的直接到如下链接去下载就可以
https://github.com/TheAlgorithms/Python
github如果无法访问的话,可以后台直接私信
算法案例可以直接访问如下链接:
https://the-algorithms.com/zh_Hans
3 部分算法代码展示
排序
Random Normal Distribution Quicksort
代码语言:python代码运行次数:0复制from random import randint
from tempfile import TemporaryFile
import numpy as np
def _in_place_quick_sort(a, start, end):
count = 0
if start < end:
pivot = randint(start, end)
temp = a[end]
a[end] = a[pivot]
a[pivot] = temp
p, count = _in_place_partition(a, start, end)
count = _in_place_quick_sort(a, start, p - 1)
count = _in_place_quick_sort(a, p 1, end)
return count
def _in_place_partition(a, start, end):
count = 0
pivot = randint(start, end)
temp = a[end]
a[end] = a[pivot]
a[pivot] = temp
new_pivot_index = start - 1
for index in range(start, end):
count = 1
if a[index] < a[end]: # check if current val is less than pivot value
new_pivot_index = new_pivot_index 1
temp = a[new_pivot_index]
a[new_pivot_index] = a[index]
a[index] = temp
temp = a[new_pivot_index 1]
a[new_pivot_index 1] = a[end]
a[end] = temp
return new_pivot_index 1, count
数据结构 哈希
代码语言:python代码运行次数:0复制#!/usr/bin/env python3
from .hash_table import HashTable
class QuadraticProbing(HashTable):
"""
Basic Hash Table example with open addressing using Quadratic Probing
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def _collision_resolution(self, key, data=None):
i = 1
new_key = self.hash_function(key i * i)
while self.values[new_key] is not None and self.values[new_key] != key:
i = 1
new_key = (
self.hash_function(key i * i)
if not self.balanced_factor() >= self.lim_charge
else None
)
if new_key is None:
break
return new_key
密码
Vigenere Cipher
代码语言:python代码运行次数:0复制LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def main() -> None:
message = input("Enter message: ")
key = input("Enter key [alphanumeric]: ")
mode = input("Encrypt/Decrypt [e/d]: ")
if mode.lower().startswith("e"):
mode = "encrypt"
translated = encrypt_message(key, message)
elif mode.lower().startswith("d"):
mode = "decrypt"
translated = decrypt_message(key, message)
print(f"n{mode.title()}ed message:")
print(translated)
def encrypt_message(key: str, message: str) -> str:
"""
>>> encrypt_message('HDarji', 'This is Harshil Darji from Dharmaj.')
'Akij ra Odrjqqs Gaisq muod Mphumrs.'
"""
return translate_message(key, message, "encrypt")
def decrypt_message(key: str, message: str) -> str:
"""
>>> decrypt_message('HDarji', 'Akij ra Odrjqqs Gaisq muod Mphumrs.')
'This is Harshil Darji from Dharmaj.'
"""
return translate_message(key, message, "decrypt")
def translate_message(key: str, message: str, mode: str) -> str:
translated = []
key_index = 0
key = key.upper()
for symbol in message:
num = LETTERS.find(symbol.upper())
if num != -1:
if mode == "encrypt":
num = LETTERS.find(key[key_index])
elif mode == "decrypt":
num -= LETTERS.find(key[key_index])
num %= len(LETTERS)
if symbol.isupper():
translated.append(LETTERS[num])
elif symbol.islower():
translated.append(LETTERS[num].lower())
key_index = 1
if key_index == len(key):
key_index = 0
else:
translated.append(symbol)
return "".join(translated)
if __name__ == "__main__":
main()
总结
实现仅用于学习目的。它们的效率可能低于 Python 标准库中的实现。可以自行决定使用它们。