mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +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
|
||||
if not 0 < rotorpos1 <= len(abc):
|
||||
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):
|
||||
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):
|
||||
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
|
||||
|
|
|
@ -62,7 +62,7 @@ class HillCipher:
|
|||
# take x and return x % len(key_string)
|
||||
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:
|
||||
"""
|
||||
|
|
|
@ -14,7 +14,7 @@ def check_prime(number):
|
|||
elif number == special_non_primes[-1]:
|
||||
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):
|
||||
|
|
|
@ -114,7 +114,7 @@ def strassen(matrix1: list, matrix2: list) -> list:
|
|||
"""
|
||||
if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]:
|
||||
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}"
|
||||
)
|
||||
dimension1 = matrix_dimensions(matrix1)
|
||||
|
|
|
@ -181,7 +181,7 @@ def _enforce_args(n: int, prices: list):
|
|||
|
||||
if n > len(prices):
|
||||
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)}"
|
||||
)
|
||||
|
||||
|
|
|
@ -55,5 +55,5 @@ if __name__ == "__main__":
|
|||
print("\n" + "".join(code))
|
||||
print(
|
||||
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(
|
||||
function
|
||||
), f"the function(object) passed should be callable your input : {function}"
|
||||
assert isinstance(a, float) or isinstance(
|
||||
a, int
|
||||
), f"a should be float or integer your input : {a}"
|
||||
assert isinstance(function(a), float) or isinstance(function(a), int), (
|
||||
assert isinstance(a, (float, int)), f"a should be float or integer your input : {a}"
|
||||
assert isinstance(function(a), (float, int)), (
|
||||
"the function should return integer or float return type of your function, "
|
||||
f"{type(a)}"
|
||||
)
|
||||
assert isinstance(b, float) or isinstance(
|
||||
b, int
|
||||
), f"b should be float or integer your input : {b}"
|
||||
assert isinstance(b, (float, int)), f"b should be float or integer your input : {b}"
|
||||
assert (
|
||||
isinstance(precision, int) and precision > 0
|
||||
), f"precision should be positive integer your input : {precision}"
|
||||
|
|
|
@ -171,7 +171,7 @@ def _verify_matrix_sizes(
|
|||
shape = _shape(matrix_a) + _shape(matrix_b)
|
||||
if shape[0] != shape[3] or shape[1] != shape[2]:
|
||||
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]})"
|
||||
)
|
||||
return (shape[0], shape[2]), (shape[1], shape[3])
|
||||
|
|
|
@ -29,7 +29,7 @@ def solution():
|
|||
triangle = f.readlines()
|
||||
|
||||
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 j in range(len(a[i])):
|
||||
|
|
|
@ -41,7 +41,7 @@ class FileSplitter:
|
|||
i += 1
|
||||
|
||||
def cleanup(self):
|
||||
map(lambda f: os.remove(f), self.block_filenames)
|
||||
map(os.remove, self.block_filenames)
|
||||
|
||||
|
||||
class NWayMerge:
|
||||
|
|
Loading…
Reference in New Issue
Block a user