-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for i in range(n): | ||
for j in range(n): | ||
y[i].append(j) | ||
|
@@ -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__": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0)) | ||
return x1 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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))]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def create_state_space_tree(sequence, current_sequence, index, index_used): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def solve(board, row): | ||
|
@@ -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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
solve(board, 0) | ||
print("The total no. of solutions are :", len(solution)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
print("grid after solving:") | ||
print_solution(solution) | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
# This function find the inverses of a i.e., a^(-1) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
# 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def check(binary): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return pi | ||
binary = list(set(temp)) | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
s = str(m % 2) + s | ||
m //= 2 | ||
temp.append(s) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def selection(chart, prime_implicants): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for i in range(len(prime_implicants)): | ||
count = prime_implicants[i].count("_") | ||
for j in range(len(binary)): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if i > 0 and ((i / 3 * 4) % 76) == 0: | ||
r = r + "\r\n" # for unix newline, put "\n" | ||
|
||
|
@@ -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): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
n = ( | ||
(base64_chars.index(s[i]) << 18) | ||
+ (base64_chars.index(s[i + 1]) << 12) | ||
|
@@ -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(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def main(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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 | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
): | ||
print("\nWARNING:") | ||
print( | ||
|
@@ -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])) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class HillCipher: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
inv_key = ( | ||
det_inv | ||
* numpy.linalg.det(self.encrypt_key) | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
row = list(map(int, input().split())) | ||
hill_matrix.append(row) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,42 +30,39 @@ def mixed_keyword(key="college", pt="UNIVERSITY"): | |
alpha = [] | ||
modalpha = [] | ||
# modalpha.append(temp) | ||
dic = dict() | ||
dic = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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")) |
There was a problem hiding this comment.
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:inline-immediately-returned-variable
)