Skip to content

Commit 07e991d

Browse files
CaedenPHcclausspre-commit-ci[bot]
authored
Add pep8-naming to pre-commit hooks and fixes incorrect naming conventions (TheAlgorithms#7062)
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (TheAlgorithms#7038) * refactor: Fix naming conventions (TheAlgorithms#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (TheAlgorithms#7038) * chore: Fix naming conventions in doctests (TheAlgorithms#7038) * fix: Temporarily disable project euler problem 104 (TheAlgorithms#7069) * chore: Fix naming conventions in doctests (TheAlgorithms#7038) Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e2cd982 commit 07e991d

File tree

140 files changed

+1555
-1539
lines changed

Some content is hidden

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

140 files changed

+1555
-1539
lines changed

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ repos:
4040
- --ignore=E203,W503
4141
- --max-complexity=25
4242
- --max-line-length=88
43+
additional_dependencies: [pep8-naming]
4344

4445
- repo: https://github.com/pre-commit/mirrors-mypy
4546
rev: v0.982

arithmetic_analysis/lu_decomposition.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from __future__ import annotations
77

88
import numpy as np
9-
import numpy.typing as NDArray
109
from numpy import float64
10+
from numpy.typing import ArrayLike
1111

1212

1313
def lower_upper_decomposition(
14-
table: NDArray[float64],
15-
) -> tuple[NDArray[float64], NDArray[float64]]:
14+
table: ArrayLike[float64],
15+
) -> tuple[ArrayLike[float64], ArrayLike[float64]]:
1616
"""Lower-Upper (LU) Decomposition
1717
1818
Example:

backtracking/n_queens.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
solution = []
1313

1414

15-
def isSafe(board: list[list[int]], row: int, column: int) -> bool:
15+
def is_safe(board: list[list[int]], row: int, column: int) -> bool:
1616
"""
1717
This function returns a boolean value True if it is safe to place a queen there
1818
considering the current state of the board.
@@ -63,7 +63,7 @@ def solve(board: list[list[int]], row: int) -> bool:
6363
If all the combinations for that particular branch are successful the board is
6464
reinitialized for the next possible combination.
6565
"""
66-
if isSafe(board, row, i):
66+
if is_safe(board, row, i):
6767
board[row][i] = 1
6868
solve(board, row + 1)
6969
board[row][i] = 0

ciphers/affine_cipher.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@
99
)
1010

1111

12-
def check_keys(keyA: int, keyB: int, mode: str) -> None:
12+
def check_keys(key_a: int, key_b: int, mode: str) -> None:
1313
if mode == "encrypt":
14-
if keyA == 1:
14+
if key_a == 1:
1515
sys.exit(
1616
"The affine cipher becomes weak when key "
1717
"A is set to 1. Choose different key"
1818
)
19-
if keyB == 0:
19+
if key_b == 0:
2020
sys.exit(
2121
"The affine cipher becomes weak when key "
2222
"B is set to 0. Choose different key"
2323
)
24-
if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1:
24+
if key_a < 0 or key_b < 0 or key_b > len(SYMBOLS) - 1:
2525
sys.exit(
2626
"Key A must be greater than 0 and key B must "
2727
f"be between 0 and {len(SYMBOLS) - 1}."
2828
)
29-
if cryptomath.gcd(keyA, len(SYMBOLS)) != 1:
29+
if cryptomath.gcd(key_a, len(SYMBOLS)) != 1:
3030
sys.exit(
31-
f"Key A {keyA} and the symbol set size {len(SYMBOLS)} "
31+
f"Key A {key_a} and the symbol set size {len(SYMBOLS)} "
3232
"are not relatively prime. Choose a different key."
3333
)
3434

@@ -39,16 +39,16 @@ def encrypt_message(key: int, message: str) -> str:
3939
... 'substitution cipher.')
4040
'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
4141
"""
42-
keyA, keyB = divmod(key, len(SYMBOLS))
43-
check_keys(keyA, keyB, "encrypt")
44-
cipherText = ""
42+
key_a, key_b = divmod(key, len(SYMBOLS))
43+
check_keys(key_a, key_b, "encrypt")
44+
cipher_text = ""
4545
for symbol in message:
4646
if symbol in SYMBOLS:
47-
symIndex = SYMBOLS.find(symbol)
48-
cipherText += SYMBOLS[(symIndex * keyA + keyB) % len(SYMBOLS)]
47+
sym_index = SYMBOLS.find(symbol)
48+
cipher_text += SYMBOLS[(sym_index * key_a + key_b) % len(SYMBOLS)]
4949
else:
50-
cipherText += symbol
51-
return cipherText
50+
cipher_text += symbol
51+
return cipher_text
5252

5353

5454
def decrypt_message(key: int, message: str) -> str:
@@ -57,25 +57,27 @@ def decrypt_message(key: int, message: str) -> str:
5757
... '{xIp~{HL}Gi')
5858
'The affine cipher is a type of monoalphabetic substitution cipher.'
5959
"""
60-
keyA, keyB = divmod(key, len(SYMBOLS))
61-
check_keys(keyA, keyB, "decrypt")
62-
plainText = ""
63-
modInverseOfkeyA = cryptomath.find_mod_inverse(keyA, len(SYMBOLS))
60+
key_a, key_b = divmod(key, len(SYMBOLS))
61+
check_keys(key_a, key_b, "decrypt")
62+
plain_text = ""
63+
mod_inverse_of_key_a = cryptomath.find_mod_inverse(key_a, len(SYMBOLS))
6464
for symbol in message:
6565
if symbol in SYMBOLS:
66-
symIndex = SYMBOLS.find(symbol)
67-
plainText += SYMBOLS[(symIndex - keyB) * modInverseOfkeyA % len(SYMBOLS)]
66+
sym_index = SYMBOLS.find(symbol)
67+
plain_text += SYMBOLS[
68+
(sym_index - key_b) * mod_inverse_of_key_a % len(SYMBOLS)
69+
]
6870
else:
69-
plainText += symbol
70-
return plainText
71+
plain_text += symbol
72+
return plain_text
7173

7274

7375
def get_random_key() -> int:
7476
while True:
75-
keyA = random.randint(2, len(SYMBOLS))
76-
keyB = random.randint(2, len(SYMBOLS))
77-
if cryptomath.gcd(keyA, len(SYMBOLS)) == 1 and keyB % len(SYMBOLS) != 0:
78-
return keyA * len(SYMBOLS) + keyB
77+
key_b = random.randint(2, len(SYMBOLS))
78+
key_b = random.randint(2, len(SYMBOLS))
79+
if cryptomath.gcd(key_b, len(SYMBOLS)) == 1 and key_b % len(SYMBOLS) != 0:
80+
return key_b * len(SYMBOLS) + key_b
7981

8082

8183
def main() -> None:

ciphers/bifid.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class BifidCipher:
1414
def __init__(self) -> None:
15-
SQUARE = [
15+
SQUARE = [ # noqa: N806
1616
["a", "b", "c", "d", "e"],
1717
["f", "g", "h", "i", "k"],
1818
["l", "m", "n", "o", "p"],

ciphers/brute_force_caesar_cipher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def decrypt(message: str) -> None:
2828
Decryption using Key #24: VOFGVWZ ROFXW
2929
Decryption using Key #25: UNEFUVY QNEWV
3030
"""
31-
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
31+
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # noqa: N806
3232
for key in range(len(LETTERS)):
3333
translated = ""
3434
for symbol in message:

ciphers/elgamal_key_generator.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def primitive_root(p_val: int) -> int:
2626

2727
def generate_key(key_size: int) -> tuple[tuple[int, int, int, int], tuple[int, int]]:
2828
print("Generating prime p...")
29-
p = rabin_miller.generateLargePrime(key_size) # select large prime number.
29+
p = rabin_miller.generate_large_prime(key_size) # select large prime number.
3030
e_1 = primitive_root(p) # one primitive root on modulo p.
3131
d = random.randrange(3, p) # private_key -> have to be greater than 2 for safety.
3232
e_2 = cryptomath.find_mod_inverse(pow(e_1, d, p), p)
@@ -37,7 +37,7 @@ def generate_key(key_size: int) -> tuple[tuple[int, int, int, int], tuple[int, i
3737
return public_key, private_key
3838

3939

40-
def make_key_files(name: str, keySize: int) -> None:
40+
def make_key_files(name: str, key_size: int) -> None:
4141
if os.path.exists(f"{name}_pubkey.txt") or os.path.exists(f"{name}_privkey.txt"):
4242
print("\nWARNING:")
4343
print(
@@ -47,16 +47,16 @@ def make_key_files(name: str, keySize: int) -> None:
4747
)
4848
sys.exit()
4949

50-
publicKey, privateKey = generate_key(keySize)
50+
public_key, private_key = generate_key(key_size)
5151
print(f"\nWriting public key to file {name}_pubkey.txt...")
5252
with open(f"{name}_pubkey.txt", "w") as fo:
5353
fo.write(
54-
"%d,%d,%d,%d" % (publicKey[0], publicKey[1], publicKey[2], publicKey[3])
54+
"%d,%d,%d,%d" % (public_key[0], public_key[1], public_key[2], public_key[3])
5555
)
5656

5757
print(f"Writing private key to file {name}_privkey.txt...")
5858
with open(f"{name}_privkey.txt", "w") as fo:
59-
fo.write("%d,%d" % (privateKey[0], privateKey[1]))
59+
fo.write("%d,%d" % (private_key[0], private_key[1]))
6060

6161

6262
def main() -> None:

ciphers/hill_cipher.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ def decrypt(self, text: str) -> str:
201201

202202

203203
def main() -> None:
204-
N = int(input("Enter the order of the encryption key: "))
204+
n = int(input("Enter the order of the encryption key: "))
205205
hill_matrix = []
206206

207207
print("Enter each row of the encryption key with space separated integers")
208-
for _ in range(N):
208+
for _ in range(n):
209209
row = [int(x) for x in input().split()]
210210
hill_matrix.append(row)
211211

ciphers/polybius.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class PolybiusCipher:
1313
def __init__(self) -> None:
14-
SQUARE = [
14+
SQUARE = [ # noqa: N806
1515
["a", "b", "c", "d", "e"],
1616
["f", "g", "h", "i", "k"],
1717
["l", "m", "n", "o", "p"],

ciphers/rabin_miller.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import random
44

55

6-
def rabinMiller(num: int) -> bool:
6+
def rabin_miller(num: int) -> bool:
77
s = num - 1
88
t = 0
99

@@ -29,7 +29,7 @@ def is_prime_low_num(num: int) -> bool:
2929
if num < 2:
3030
return False
3131

32-
lowPrimes = [
32+
low_primes = [
3333
2,
3434
3,
3535
5,
@@ -200,24 +200,24 @@ def is_prime_low_num(num: int) -> bool:
200200
997,
201201
]
202202

203-
if num in lowPrimes:
203+
if num in low_primes:
204204
return True
205205

206-
for prime in lowPrimes:
206+
for prime in low_primes:
207207
if (num % prime) == 0:
208208
return False
209209

210-
return rabinMiller(num)
210+
return rabin_miller(num)
211211

212212

213-
def generateLargePrime(keysize: int = 1024) -> int:
213+
def generate_large_prime(keysize: int = 1024) -> int:
214214
while True:
215215
num = random.randrange(2 ** (keysize - 1), 2 ** (keysize))
216216
if is_prime_low_num(num):
217217
return num
218218

219219

220220
if __name__ == "__main__":
221-
num = generateLargePrime()
221+
num = generate_large_prime()
222222
print(("Prime number:", num))
223223
print(("is_prime_low_num:", is_prime_low_num(num)))

ciphers/rsa_cipher.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ def get_text_from_blocks(
3737

3838

3939
def encrypt_message(
40-
message: str, key: tuple[int, int], blockSize: int = DEFAULT_BLOCK_SIZE
40+
message: str, key: tuple[int, int], block_size: int = DEFAULT_BLOCK_SIZE
4141
) -> list[int]:
4242
encrypted_blocks = []
4343
n, e = key
4444

45-
for block in get_blocks_from_text(message, blockSize):
45+
for block in get_blocks_from_text(message, block_size):
4646
encrypted_blocks.append(pow(block, e, n))
4747
return encrypted_blocks
4848

@@ -63,8 +63,8 @@ def decrypt_message(
6363
def read_key_file(key_filename: str) -> tuple[int, int, int]:
6464
with open(key_filename) as fo:
6565
content = fo.read()
66-
key_size, n, EorD = content.split(",")
67-
return (int(key_size), int(n), int(EorD))
66+
key_size, n, eor_d = content.split(",")
67+
return (int(key_size), int(n), int(eor_d))
6868

6969

7070
def encrypt_and_write_to_file(
@@ -125,15 +125,15 @@ def main() -> None:
125125

126126
if mode == "encrypt":
127127
if not os.path.exists("rsa_pubkey.txt"):
128-
rkg.makeKeyFiles("rsa", 1024)
128+
rkg.make_key_files("rsa", 1024)
129129

130130
message = input("\nEnter message: ")
131131
pubkey_filename = "rsa_pubkey.txt"
132132
print(f"Encrypting and writing to {filename}...")
133-
encryptedText = encrypt_and_write_to_file(filename, pubkey_filename, message)
133+
encrypted_text = encrypt_and_write_to_file(filename, pubkey_filename, message)
134134

135135
print("\nEncrypted text:")
136-
print(encryptedText)
136+
print(encrypted_text)
137137

138138
elif mode == "decrypt":
139139
privkey_filename = "rsa_privkey.txt"

ciphers/rsa_factorization.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import random
1414

1515

16-
def rsafactor(d: int, e: int, N: int) -> list[int]:
16+
def rsafactor(d: int, e: int, n: int) -> list[int]:
1717
"""
1818
This function returns the factors of N, where p*q=N
1919
Return: [p, q]
@@ -35,16 +35,16 @@ def rsafactor(d: int, e: int, N: int) -> list[int]:
3535
p = 0
3636
q = 0
3737
while p == 0:
38-
g = random.randint(2, N - 1)
38+
g = random.randint(2, n - 1)
3939
t = k
4040
while True:
4141
if t % 2 == 0:
4242
t = t // 2
43-
x = (g**t) % N
44-
y = math.gcd(x - 1, N)
43+
x = (g**t) % n
44+
y = math.gcd(x - 1, n)
4545
if x > 1 and y > 1:
4646
p = y
47-
q = N // y
47+
q = n // y
4848
break # find the correct factors
4949
else:
5050
break # t is not divisible by 2, break and choose another g

0 commit comments

Comments
 (0)