Skip to content

Commit 95398e0

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents 9bd4765 + b5cb1fb commit 95398e0

File tree

314 files changed

+681
-364
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

314 files changed

+681
-364
lines changed

.github/workflows/ruff.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ jobs:
1313
steps:
1414
- uses: actions/checkout@v4
1515
- run: pip install --user ruff
16-
- run: ruff --output-format=github .
16+
- run: ruff check --output-format=github .

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.2.2
19+
rev: v0.3.4
2020
hooks:
2121
- id: ruff
2222
- id: ruff-format
@@ -47,7 +47,7 @@ repos:
4747
- id: validate-pyproject
4848

4949
- repo: https://github.com/pre-commit/mirrors-mypy
50-
rev: v1.8.0
50+
rev: v1.9.0
5151
hooks:
5252
- id: mypy
5353
args:

DIRECTORY.md

+7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
* [Run Length Encoding](compression/run_length_encoding.py)
135135

136136
## Computer Vision
137+
* [Cnn Classification](computer_vision/cnn_classification.py)
137138
* [Flip Augmentation](computer_vision/flip_augmentation.py)
138139
* [Haralick Descriptors](computer_vision/haralick_descriptors.py)
139140
* [Harris Corner](computer_vision/harris_corner.py)
@@ -344,6 +345,7 @@
344345
* [Floyd Warshall](dynamic_programming/floyd_warshall.py)
345346
* [Integer Partition](dynamic_programming/integer_partition.py)
346347
* [Iterating Through Submasks](dynamic_programming/iterating_through_submasks.py)
348+
* [K Means Clustering Tensorflow](dynamic_programming/k_means_clustering_tensorflow.py)
347349
* [Knapsack](dynamic_programming/knapsack.py)
348350
* [Largest Divisible Subset](dynamic_programming/largest_divisible_subset.py)
349351
* [Longest Common Subsequence](dynamic_programming/longest_common_subsequence.py)
@@ -417,6 +419,7 @@
417419
* [Koch Snowflake](fractals/koch_snowflake.py)
418420
* [Mandelbrot](fractals/mandelbrot.py)
419421
* [Sierpinski Triangle](fractals/sierpinski_triangle.py)
422+
* [Vicsek](fractals/vicsek.py)
420423

421424
## Fuzzy Logic
422425
* [Fuzzy Operations](fuzzy_logic/fuzzy_operations.py)
@@ -571,6 +574,8 @@
571574
* [Local Weighted Learning](machine_learning/local_weighted_learning/local_weighted_learning.py)
572575
* [Logistic Regression](machine_learning/logistic_regression.py)
573576
* [Loss Functions](machine_learning/loss_functions.py)
577+
* Lstm
578+
* [Lstm Prediction](machine_learning/lstm/lstm_prediction.py)
574579
* [Mfcc](machine_learning/mfcc.py)
575580
* [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py)
576581
* [Polynomial Regression](machine_learning/polynomial_regression.py)
@@ -674,6 +679,7 @@
674679
* [Newton Forward Interpolation](maths/numerical_analysis/newton_forward_interpolation.py)
675680
* [Newton Raphson](maths/numerical_analysis/newton_raphson.py)
676681
* [Numerical Integration](maths/numerical_analysis/numerical_integration.py)
682+
* [Proper Fractions](maths/numerical_analysis/proper_fractions.py)
677683
* [Runge Kutta](maths/numerical_analysis/runge_kutta.py)
678684
* [Runge Kutta Fehlberg 45](maths/numerical_analysis/runge_kutta_fehlberg_45.py)
679685
* [Runge Kutta Gills](maths/numerical_analysis/runge_kutta_gills.py)
@@ -801,6 +807,7 @@
801807
* [Swish](neural_network/activation_functions/swish.py)
802808
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
803809
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
810+
* [Input Data](neural_network/input_data.py)
804811
* [Simple Neural Network](neural_network/simple_neural_network.py)
805812

806813
## Other

backtracking/all_combinations.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""
2-
In this problem, we want to determine all possible combinations of k
3-
numbers out of 1 ... n. We use backtracking to solve this problem.
2+
In this problem, we want to determine all possible combinations of k
3+
numbers out of 1 ... n. We use backtracking to solve this problem.
44
5-
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!))),
5+
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!))),
66
"""
7+
78
from __future__ import annotations
89

910
from itertools import combinations

backtracking/all_permutations.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""
2-
In this problem, we want to determine all possible permutations
3-
of the given sequence. We use backtracking to solve this problem.
2+
In this problem, we want to determine all possible permutations
3+
of the given sequence. We use backtracking to solve this problem.
44
5-
Time complexity: O(n! * n),
6-
where n denotes the length of the given sequence.
5+
Time complexity: O(n! * n),
6+
where n denotes the length of the given sequence.
77
"""
8+
89
from __future__ import annotations
910

1011

backtracking/all_subsequences.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Time complexity: O(2^n),
66
where n denotes the length of the given sequence.
77
"""
8+
89
from __future__ import annotations
910

1011
from typing import Any

backtracking/coloring.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
Graph Coloring also called "m coloring problem"
3-
consists of coloring a given graph with at most m colors
4-
such that no adjacent vertices are assigned the same color
2+
Graph Coloring also called "m coloring problem"
3+
consists of coloring a given graph with at most m colors
4+
such that no adjacent vertices are assigned the same color
55
6-
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
6+
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
77
"""
88

99

backtracking/hamiltonian_cycle.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
2-
A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
3-
through a graph that visits each node exactly once.
4-
Determining whether such paths and cycles exist in graphs
5-
is the 'Hamiltonian path problem', which is NP-complete.
2+
A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
3+
through a graph that visits each node exactly once.
4+
Determining whether such paths and cycles exist in graphs
5+
is the 'Hamiltonian path problem', which is NP-complete.
66
7-
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
7+
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
88
"""
99

1010

backtracking/minimax.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
leaves of game tree is stored in scores[]
88
height is maximum height of Game tree
99
"""
10+
1011
from __future__ import annotations
1112

1213
import math

backtracking/n_queens.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""
22
3-
The nqueens problem is of placing N queens on a N * N
4-
chess board such that no queen can attack any other queens placed
5-
on that chess board.
6-
This means that one queen cannot have any other queen on its horizontal, vertical and
7-
diagonal lines.
3+
The nqueens problem is of placing N queens on a N * N
4+
chess board such that no queen can attack any other queens placed
5+
on that chess board.
6+
This means that one queen cannot have any other queen on its horizontal, vertical and
7+
diagonal lines.
88
99
"""
10+
1011
from __future__ import annotations
1112

1213
solution = []

backtracking/n_queens_math.py

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
for another one or vice versa.
7676
7777
"""
78+
7879
from __future__ import annotations
7980

8081

backtracking/sudoku.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
have solved the puzzle. else, we backtrack and place another number
1010
in that cell and repeat this process.
1111
"""
12+
1213
from __future__ import annotations
1314

1415
Matrix = list[list[int]]

backtracking/sum_of_subsets.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""
2-
The sum-of-subsetsproblem states that a set of non-negative integers, and a
3-
value M, determine all possible subsets of the given set whose summation sum
4-
equal to given M.
2+
The sum-of-subsetsproblem states that a set of non-negative integers, and a
3+
value M, determine all possible subsets of the given set whose summation sum
4+
equal to given M.
55
6-
Summation of the chosen numbers must be equal to given number M and one number
7-
can be used only once.
6+
Summation of the chosen numbers must be equal to given number M and one number
7+
can be used only once.
88
"""
9+
910
from __future__ import annotations
1011

1112

boolean_algebra/nor_gate.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Code provided by Akshaj Vishwanathan
1313
https://www.geeksforgeeks.org/logic-gates-in-python
1414
"""
15+
1516
from collections.abc import Callable
1617

1718

cellular_automata/conways_game_of_life.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Conway's Game of Life implemented in Python.
33
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
44
"""
5+
56
from __future__ import annotations
67

78
from PIL import Image

cellular_automata/game_of_life.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
4.
2727
Any dead cell with exactly three live neighbours be-
2828
comes a live cell, as if by reproduction.
29-
"""
29+
"""
30+
3031
import random
3132
import sys
3233

cellular_automata/nagel_schrekenberg.py

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
>>> simulate(construct_highway(5, 2, -2), 3, 0, 2)
2525
[[0, -1, 0, -1, 0], [0, -1, 0, -1, -1], [0, -1, -1, 1, -1], [-1, 1, -1, 0, -1]]
2626
"""
27+
2728
from random import randint, random
2829

2930

ciphers/a1z26.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
https://www.dcode.fr/letter-number-cipher
66
http://bestcodes.weebly.com/a1z26.html
77
"""
8+
89
from __future__ import annotations
910

1011

ciphers/atbash.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
""" https://en.wikipedia.org/wiki/Atbash """
1+
"""https://en.wikipedia.org/wiki/Atbash"""
2+
23
import string
34

45

ciphers/base32.py

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
https://en.wikipedia.org/wiki/Base32
55
"""
6+
67
B32_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
78

89

ciphers/enigma_machine2.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
Created by TrapinchO
1616
"""
17+
1718
from __future__ import annotations
1819

1920
RotorPositionT = tuple[int, int, int]

ciphers/fractionated_morse_cipher.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
http://practicalcryptography.com/ciphers/fractionated-morse-cipher/
1010
"""
11+
1112
import string
1213

1314
MORSE_CODE_DICT = {

ciphers/hill_cipher.py

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
https://www.youtube.com/watch?v=4RhLNDqcjpA
3636
3737
"""
38+
3839
import string
3940

4041
import numpy

ciphers/permutation_cipher.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
For more info: https://www.nku.edu/~christensen/1402%20permutation%20ciphers.pdf
99
"""
10+
1011
import random
1112

1213

ciphers/rail_fence_cipher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" https://en.wikipedia.org/wiki/Rail_fence_cipher """
1+
"""https://en.wikipedia.org/wiki/Rail_fence_cipher"""
22

33

44
def encrypt(input_string: str, key: int) -> str:

ciphers/rsa_cipher.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,9 @@ def encrypt_and_write_to_file(
7676
key_size, n, e = read_key_file(key_filename)
7777
if key_size < block_size * 8:
7878
sys.exit(
79-
"ERROR: Block size is {} bits and key size is {} bits. The RSA cipher "
80-
"requires the block size to be equal to or greater than the key size. "
81-
"Either decrease the block size or use different keys.".format(
82-
block_size * 8, key_size
83-
)
79+
f"ERROR: Block size is {block_size * 8} bits and key size is {key_size} "
80+
"bits. The RSA cipher requires the block size to be equal to or greater "
81+
"than the key size. Either decrease the block size or use different keys."
8482
)
8583

8684
encrypted_blocks = [str(i) for i in encrypt_message(message, (n, e), block_size)]
@@ -102,11 +100,9 @@ def read_from_file_and_decrypt(message_filename: str, key_filename: str) -> str:
102100

103101
if key_size < block_size * 8:
104102
sys.exit(
105-
"ERROR: Block size is {} bits and key size is {} bits. The RSA cipher "
106-
"requires the block size to be equal to or greater than the key size. "
107-
"Did you specify the correct key file and encrypted file?".format(
108-
block_size * 8, key_size
109-
)
103+
f"ERROR: Block size is {block_size * 8} bits and key size is {key_size} "
104+
"bits. The RSA cipher requires the block size to be equal to or greater "
105+
"than the key size. Were the correct key file and encrypted file specified?"
110106
)
111107

112108
encrypted_blocks = []

ciphers/rsa_factorization.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
More readable source: https://www.di-mgt.com.au/rsa_factorize_n.html
88
large number can take minutes to factor, therefore are not included in doctest.
99
"""
10+
1011
from __future__ import annotations
1112

1213
import math

ciphers/xor_cipher.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
"""
2-
author: Christian Bender
3-
date: 21.12.2017
4-
class: XORCipher
5-
6-
This class implements the XOR-cipher algorithm and provides
7-
some useful methods for encrypting and decrypting strings and
8-
files.
9-
10-
Overview about methods
11-
12-
- encrypt : list of char
13-
- decrypt : list of char
14-
- encrypt_string : str
15-
- decrypt_string : str
16-
- encrypt_file : boolean
17-
- decrypt_file : boolean
2+
author: Christian Bender
3+
date: 21.12.2017
4+
class: XORCipher
5+
6+
This class implements the XOR-cipher algorithm and provides
7+
some useful methods for encrypting and decrypting strings and
8+
files.
9+
10+
Overview about methods
11+
12+
- encrypt : list of char
13+
- decrypt : list of char
14+
- encrypt_string : str
15+
- decrypt_string : str
16+
- encrypt_file : boolean
17+
- decrypt_file : boolean
1818
"""
19+
1920
from __future__ import annotations
2021

2122

compression/burrows_wheeler.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
original character. The BWT is thus a "free" method of improving the efficiency
1111
of text compression algorithms, costing only some extra computation.
1212
"""
13+
1314
from __future__ import annotations
1415

1516
from typing import TypedDict

compression/lempel_ziv.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
One of the several implementations of Lempel–Ziv–Welch compression algorithm
3-
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
2+
One of the several implementations of Lempel–Ziv–Welch compression algorithm
3+
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
44
"""
55

66
import math

compression/lempel_ziv_decompress.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
One of the several implementations of Lempel–Ziv–Welch decompression algorithm
3-
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
2+
One of the several implementations of Lempel–Ziv–Welch decompression algorithm
3+
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
44
"""
55

66
import math

0 commit comments

Comments
 (0)