mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
Improve code on f-strings and brevity (#6126)
* Update strassen_matrix_multiplication.py * Update matrix_operation.py * Update enigma_machine2.py * Update enigma_machine.py * Update enigma_machine2.py * Update rod_cutting.py * Update external_sort.py * Update sol1.py * Update hill_cipher.py * Update prime_numbers.py * Update integration_by_simpson_approx.py
This commit is contained in:
parent
e95ecfaf27
commit
dbee5f072f
|
@ -94,15 +94,15 @@ def _validator(
|
||||||
rotorpos1, rotorpos2, rotorpos3 = rotpos
|
rotorpos1, rotorpos2, rotorpos3 = rotpos
|
||||||
if not 0 < rotorpos1 <= len(abc):
|
if not 0 < rotorpos1 <= len(abc):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"First rotor position is not within range of 1..26 (" f"{rotorpos1}"
|
"First rotor position is not within range of 1..26 (" f"{rotorpos1}"
|
||||||
)
|
)
|
||||||
if not 0 < rotorpos2 <= len(abc):
|
if not 0 < rotorpos2 <= len(abc):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Second rotor position is not within range of 1..26 (" f"{rotorpos2})"
|
"Second rotor position is not within range of 1..26 (" f"{rotorpos2})"
|
||||||
)
|
)
|
||||||
if not 0 < rotorpos3 <= len(abc):
|
if not 0 < rotorpos3 <= len(abc):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Third rotor position is not within range of 1..26 (" f"{rotorpos3})"
|
"Third rotor position is not within range of 1..26 (" f"{rotorpos3})"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Validates string and returns dict
|
# Validates string and returns dict
|
||||||
|
|
|
@ -62,7 +62,7 @@ class HillCipher:
|
||||||
# take x and return x % len(key_string)
|
# take x and return x % len(key_string)
|
||||||
modulus = numpy.vectorize(lambda x: x % 36)
|
modulus = numpy.vectorize(lambda x: x % 36)
|
||||||
|
|
||||||
to_int = numpy.vectorize(lambda x: round(x))
|
to_int = numpy.vectorize(round)
|
||||||
|
|
||||||
def __init__(self, encrypt_key: numpy.ndarray) -> None:
|
def __init__(self, encrypt_key: numpy.ndarray) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -14,7 +14,7 @@ def check_prime(number):
|
||||||
elif number == special_non_primes[-1]:
|
elif number == special_non_primes[-1]:
|
||||||
return 3
|
return 3
|
||||||
|
|
||||||
return all([number % i for i in range(2, number)])
|
return all(number % i for i in range(2, number))
|
||||||
|
|
||||||
|
|
||||||
def next_prime(value, factor=1, **kwargs):
|
def next_prime(value, factor=1, **kwargs):
|
||||||
|
|
|
@ -114,7 +114,7 @@ def strassen(matrix1: list, matrix2: list) -> list:
|
||||||
"""
|
"""
|
||||||
if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]:
|
if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
f"Unable to multiply these matrices, please check the dimensions. \n"
|
"Unable to multiply these matrices, please check the dimensions. \n"
|
||||||
f"Matrix A:{matrix1} \nMatrix B:{matrix2}"
|
f"Matrix A:{matrix1} \nMatrix B:{matrix2}"
|
||||||
)
|
)
|
||||||
dimension1 = matrix_dimensions(matrix1)
|
dimension1 = matrix_dimensions(matrix1)
|
||||||
|
|
|
@ -181,7 +181,7 @@ def _enforce_args(n: int, prices: list):
|
||||||
|
|
||||||
if n > len(prices):
|
if n > len(prices):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Each integral piece of rod must have a corresponding "
|
"Each integral piece of rod must have a corresponding "
|
||||||
f"price. Got n = {n} but length of prices = {len(prices)}"
|
f"price. Got n = {n} but length of prices = {len(prices)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -55,5 +55,5 @@ if __name__ == "__main__":
|
||||||
print("\n" + "".join(code))
|
print("\n" + "".join(code))
|
||||||
print(
|
print(
|
||||||
f"\nYour Token is {token} please write it down.\nIf you want to decode "
|
f"\nYour Token is {token} please write it down.\nIf you want to decode "
|
||||||
f"this message again you should input same digits as token!"
|
"this message again you should input same digits as token!"
|
||||||
)
|
)
|
||||||
|
|
|
@ -92,16 +92,12 @@ def simpson_integration(function, a: float, b: float, precision: int = 4) -> flo
|
||||||
assert callable(
|
assert callable(
|
||||||
function
|
function
|
||||||
), f"the function(object) passed should be callable your input : {function}"
|
), f"the function(object) passed should be callable your input : {function}"
|
||||||
assert isinstance(a, float) or isinstance(
|
assert isinstance(a, (float, int)), f"a should be float or integer your input : {a}"
|
||||||
a, int
|
assert isinstance(function(a), (float, int)), (
|
||||||
), f"a should be float or integer your input : {a}"
|
|
||||||
assert isinstance(function(a), float) or isinstance(function(a), int), (
|
|
||||||
"the function should return integer or float return type of your function, "
|
"the function should return integer or float return type of your function, "
|
||||||
f"{type(a)}"
|
f"{type(a)}"
|
||||||
)
|
)
|
||||||
assert isinstance(b, float) or isinstance(
|
assert isinstance(b, (float, int)), f"b should be float or integer your input : {b}"
|
||||||
b, int
|
|
||||||
), f"b should be float or integer your input : {b}"
|
|
||||||
assert (
|
assert (
|
||||||
isinstance(precision, int) and precision > 0
|
isinstance(precision, int) and precision > 0
|
||||||
), f"precision should be positive integer your input : {precision}"
|
), f"precision should be positive integer your input : {precision}"
|
||||||
|
|
|
@ -171,7 +171,7 @@ def _verify_matrix_sizes(
|
||||||
shape = _shape(matrix_a) + _shape(matrix_b)
|
shape = _shape(matrix_a) + _shape(matrix_b)
|
||||||
if shape[0] != shape[3] or shape[1] != shape[2]:
|
if shape[0] != shape[3] or shape[1] != shape[2]:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"operands could not be broadcast together with shape "
|
"operands could not be broadcast together with shape "
|
||||||
f"({shape[0], shape[1]}), ({shape[2], shape[3]})"
|
f"({shape[0], shape[1]}), ({shape[2], shape[3]})"
|
||||||
)
|
)
|
||||||
return (shape[0], shape[2]), (shape[1], shape[3])
|
return (shape[0], shape[2]), (shape[1], shape[3])
|
||||||
|
|
|
@ -29,7 +29,7 @@ def solution():
|
||||||
triangle = f.readlines()
|
triangle = f.readlines()
|
||||||
|
|
||||||
a = map(lambda x: x.rstrip("\r\n").split(" "), triangle)
|
a = map(lambda x: x.rstrip("\r\n").split(" "), triangle)
|
||||||
a = list(map(lambda x: list(map(lambda y: int(y), x)), a))
|
a = list(map(lambda x: list(map(int, x)), a))
|
||||||
|
|
||||||
for i in range(1, len(a)):
|
for i in range(1, len(a)):
|
||||||
for j in range(len(a[i])):
|
for j in range(len(a[i])):
|
||||||
|
|
|
@ -41,7 +41,7 @@ class FileSplitter:
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
map(lambda f: os.remove(f), self.block_filenames)
|
map(os.remove, self.block_filenames)
|
||||||
|
|
||||||
|
|
||||||
class NWayMerge:
|
class NWayMerge:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user