Skip to content

Sourcery refactored master branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions arithmetic_analysis/gaussian_elimination.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ def gaussian_elimination(coefficients: np.matrix, vector: np.array) -> np.array:
factor = augmented_mat[col, row] / pivot
augmented_mat[col, :] -= factor * augmented_mat[row, :]

x = retroactive_resolution(
return retroactive_resolution(
augmented_mat[:, 0:columns], augmented_mat[:, columns : columns + 1]
)

return x
Comment on lines -73 to -77
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function gaussian_elimination refactored with the following changes:



if __name__ == "__main__":
import doctest
Expand Down
6 changes: 2 additions & 4 deletions arithmetic_analysis/newton_forward_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def ucal(u, p):

def main():
n = int(input("enter the numbers of values"))
y = []
for i in range(n):
y.append([])
y = [[] for _ in range(n)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

for i in range(n):
for j in range(n):
y[i].append(j)
Expand All @@ -48,7 +46,7 @@ def main():
for i in range(1, n):
summ += (ucal(u, i) * y[0][i]) / math.factorial(i)

print("the value at {} is {}".format(value, summ))
print(f"the value at {value} is {summ}")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion arithmetic_analysis/secant_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def SecantMethod(lower_bound, upper_bound, repeats):
"""
x0 = lower_bound
x1 = upper_bound
for i in range(0, repeats):
for _ in range(0, repeats):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SecantMethod refactored with the following changes:

x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
return x1

Expand Down
2 changes: 1 addition & 1 deletion backtracking/all_permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def generate_all_permutations(sequence):
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
create_state_space_tree(sequence, [], 0, [0 for _ in range(len(sequence))])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function generate_all_permutations refactored with the following changes:



def create_state_space_tree(sequence, current_sequence, index, index_used):
Expand Down
10 changes: 5 additions & 5 deletions backtracking/n_queens.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def isSafe(board, row, column):
for i, j in zip(range(row, -1, -1), range(column, -1, -1)):
if board[i][j] == 1:
return False
for i, j in zip(range(row, -1, -1), range(column, len(board))):
if board[i][j] == 1:
return False
return True
return all(
board[i][j] != 1
for i, j in zip(range(row, -1, -1), range(column, len(board)))
)
Comment on lines -35 to +38
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function isSafe refactored with the following changes:



def solve(board, row):
Expand Down Expand Up @@ -83,6 +83,6 @@ def printboard(board):

# n=int(input("The no. of queens"))
n = 8
board = [[0 for i in range(n)] for j in range(n)]
board = [[0 for _ in range(n)] for _ in range(n)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 86-86 refactored with the following changes:

solve(board, 0)
print("The total no. of solutions are :", len(solution))
3 changes: 1 addition & 2 deletions backtracking/sudoku.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ def print_solution(grid):
# make a copy of grid so that you can compare with the unmodified grid
for grid in (initial_grid, no_solution):
grid = list(map(list, grid))
solution = sudoku(grid)
if solution:
if solution := sudoku(grid):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 146-147 refactored with the following changes:

print("grid after solving:")
print_solution(solution)
else:
Expand Down
6 changes: 2 additions & 4 deletions blockchain/modular_division.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def modular_division(a, b, n):
"""
assert n > 1 and a > 0 and greatest_common_divisor(a, n) == 1
(d, t, s) = extended_gcd(n, a) # Implemented below
x = (b * s) % n
return x
return (b * s) % n
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function modular_division refactored with the following changes:



# This function find the inverses of a i.e., a^(-1)
Expand Down Expand Up @@ -64,8 +63,7 @@ def modular_division2(a, b, n):

"""
s = invert_modulo(a, n)
x = (b * s) % n
return x
return (b * s) % n
Comment on lines -67 to +66
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function modular_division2 refactored with the following changes:



# Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x and y, then d = gcd(a,b)
Expand Down
25 changes: 7 additions & 18 deletions boolean_algebra/quine_mc_cluskey.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ def compare_string(string1, string2):
if l1[i] != l2[i]:
count += 1
l1[i] = "_"
if count > 1:
return -1
else:
return "".join(l1)
return -1 if count > 1 else "".join(l1)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function compare_string refactored with the following changes:



def check(binary):
Expand All @@ -35,10 +32,8 @@ def check(binary):
check1[i] = "*"
check1[j] = "*"
temp.append(k)
for i in range(len(binary)):
if check1[i] == "$":
pi.append(binary[i])
if len(temp) == 0:
pi.extend(binary[i] for i in range(len(binary)) if check1[i] == "$")
if not temp:
Comment on lines -38 to +36
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function check refactored with the following changes:

return pi
binary = list(set(temp))

Expand All @@ -51,7 +46,7 @@ def decimal_to_binary(no_of_variable, minterms):
temp = []
s = ""
for m in minterms:
for i in range(no_of_variable):
for _ in range(no_of_variable):
Comment on lines -54 to +49
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function decimal_to_binary refactored with the following changes:

s = str(m % 2) + s
m //= 2
temp.append(s)
Expand All @@ -69,14 +64,8 @@ def is_for_table(string1, string2, count):
"""
l1 = list(string1)
l2 = list(string2)
count_n = 0
for i in range(len(l1)):
if l1[i] != l2[i]:
count_n += 1
if count_n == count:
return True
else:
return False
count_n = sum(1 for i in range(len(l1)) if l1[i] != l2[i])
return count_n == count
Comment on lines -72 to +68
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function is_for_table refactored with the following changes:



def selection(chart, prime_implicants):
Expand Down Expand Up @@ -131,7 +120,7 @@ def prime_implicant_chart(prime_implicants, binary):
>>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5'])
[[1]]
"""
chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))]
chart = [[0 for _ in range(len(binary))] for _ in range(len(prime_implicants))]
Comment on lines -134 to +123
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prime_implicant_chart refactored with the following changes:

for i in range(len(prime_implicants)):
count = prime_implicants[i].count("_")
for j in range(len(binary)):
Expand Down
6 changes: 2 additions & 4 deletions ciphers/affine_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ def checkKeys(keyA, keyB, mode):
)
if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1:
sys.exit(
"Key A must be greater than 0 and key B must be between 0 and %s."
% (len(SYMBOLS) - 1)
f"Key A must be greater than 0 and key B must be between 0 and {len(SYMBOLS) - 1}."
)
if cryptoMath.gcd(keyA, len(SYMBOLS)) != 1:
sys.exit(
"Key A %s and the symbol set size %s are not relatively prime. Choose a different key."
% (keyA, len(SYMBOLS))
f"Key A {keyA} and the symbol set size {len(SYMBOLS)} are not relatively prime. Choose a different key."
Comment on lines -37 to +41
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function checkKeys refactored with the following changes:

)


Expand Down
24 changes: 8 additions & 16 deletions ciphers/base64_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def encode_base64(text):
p = "=" * c # the padding
s = byte_text + b"\x00" * c # the text to encode

i = 0
while i < len(s):
for i in range(0, len(s), 3):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function encode_base64 refactored with the following changes:

if i > 0 and ((i / 3 * 4) % 76) == 0:
r = r + "\r\n" # for unix newline, put "\n"

Expand All @@ -28,9 +27,7 @@ def encode_base64(text):
n4 = n & 63

r += base64_chars[n1] + base64_chars[n2] + base64_chars[n3] + base64_chars[n4]
i += 3

return r[0 : len(r) - len(p)] + p
return r[:len(r) - len(p)] + p


def decode_base64(text):
Expand All @@ -49,22 +46,19 @@ def decode_base64(text):
if i in base64_chars:
s += i
c = ""
else:
if i == "=":
c += "="
elif i == "=":
c += "="

p = ""
if c == "=":
p = "A"
else:
if c == "==":
p = "AA"
elif c == "==":
p = "AA"

r = b""
s = s + p

i = 0
while i < len(s):
for i in range(0, len(s), 4):
Comment on lines -52 to +61
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function decode_base64 refactored with the following changes:

n = (
(base64_chars.index(s[i]) << 18)
+ (base64_chars.index(s[i + 1]) << 12)
Expand All @@ -74,9 +68,7 @@ def decode_base64(text):

r += bytes([(n >> 16) & 255]) + bytes([(n >> 8) & 255]) + bytes([n & 255])

i += 4

return str(r[0 : len(r) - len(p)], "utf-8")
return str(r[:len(r) - len(p)], "utf-8")


def main():
Expand Down
4 changes: 2 additions & 2 deletions ciphers/brute_force_caesar_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def decrypt(message):
for symbol in message:
if symbol in LETTERS:
num = LETTERS.find(symbol)
num = num - key
num -= key
if num < 0:
num = num + len(LETTERS)
translated = translated + LETTERS[num]
else:
translated = translated + symbol
print("Decryption using Key #%s: %s" % (key, translated))
print(f"Decryption using Key #{key}: {translated}")
Comment on lines -37 to +43
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function decrypt refactored with the following changes:



def main():
Expand Down
4 changes: 1 addition & 3 deletions ciphers/caesar_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ def decrypt(input_string: str, key: int) -> str:


def brute_force(input_string: str) -> None:
key = 1
result = ""
while key <= 94:
for key in range(1, 95):
Comment on lines -26 to +27
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function brute_force refactored with the following changes:

for x in input_string:
indx = (ord(x) - key) % 256
if indx < 32:
indx = indx + 95
result = result + chr(indx)
print(f"Key: {key}\t| Message: {result}")
result = ""
key += 1
return None


Expand Down
10 changes: 5 additions & 5 deletions ciphers/elgamal_key_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def generateKey(keySize):


def makeKeyFiles(name, keySize):
if os.path.exists("%s_pubkey.txt" % name) or os.path.exists(
"%s_privkey.txt" % name
if os.path.exists(f"{name}_pubkey.txt") or os.path.exists(
f"{name}_privkey.txt"
Comment on lines -44 to +45
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function makeKeyFiles refactored with the following changes:

):
print("\nWARNING:")
print(
Expand All @@ -54,13 +54,13 @@ def makeKeyFiles(name, keySize):

publicKey, privateKey = generateKey(keySize)
print("\nWriting public key to file %s_pubkey.txt..." % name)
with open("%s_pubkey.txt" % name, "w") as fo:
with open(f"{name}_pubkey.txt", "w") as fo:
fo.write(
"%d,%d,%d,%d" % (publicKey[0], publicKey[1], publicKey[2], publicKey[3])
)

print("Writing private key to file %s_privkey.txt..." % name)
with open("%s_privkey.txt" % name, "w") as fo:
print(f"Writing private key to file {name}_privkey.txt...")
with open(f"{name}_privkey.txt", "w") as fo:
fo.write("%d,%d" % (privateKey[0], privateKey[1]))


Expand Down
20 changes: 10 additions & 10 deletions ciphers/hill_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@


def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
return b if a == 0 else gcd(b % a, a)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function gcd refactored with the following changes:



class HillCipher:
Expand Down Expand Up @@ -114,12 +112,14 @@ def makeDecryptKey(self):

if det < 0:
det = det % len(self.key_string)
det_inv = None
for i in range(len(self.key_string)):
if (det * i) % len(self.key_string) == 1:
det_inv = i
break

det_inv = next(
(
i
for i in range(len(self.key_string))
if (det * i) % len(self.key_string) == 1
),
None,
)
Comment on lines -117 to +122
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HillCipher.makeDecryptKey refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)

inv_key = (
det_inv
* numpy.linalg.det(self.encrypt_key)
Expand Down Expand Up @@ -151,7 +151,7 @@ def main():
hill_matrix = []

print("Enter each row of the encryption key with space separated integers")
for i in range(N):
for _ in range(N):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

row = list(map(int, input().split()))
hill_matrix.append(row)

Expand Down
19 changes: 8 additions & 11 deletions ciphers/mixed_keyword_cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,39 @@ def mixed_keyword(key="college", pt="UNIVERSITY"):
alpha = []
modalpha = []
# modalpha.append(temp)
dic = dict()
dic = {}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function mixed_keyword refactored with the following changes:

c = 0
for i in range(65, 91):
t = chr(i)
alpha.append(t)
if t not in temp:
temp.append(t)
# print(temp)
r = int(26 / 4)
r = 26 // 4
# print(r)
k = 0
for i in range(r):
for _ in range(r):
t = []
for j in range(l):
for _ in range(l):
t.append(temp[k])
if not (k < 25):
if k >= 25:
break
k += 1
modalpha.append(t)
# print(modalpha)
d = dict()
d = {}
j = 0
k = 0
for j in range(l):
for i in modalpha:
if not (len(i) - 1 >= j):
break
d[alpha[k]] = i[j]
if not k < 25:
if k >= 25:
break
k += 1
print(d)
cypher = ""
for i in pt:
cypher += d[i]
return cypher
return "".join(d[i] for i in pt)


print(mixed_keyword("college", "UNIVERSITY"))
Loading