From dbee5f072f68c57bce3443e5ed07fe496ba9d76d Mon Sep 17 00:00:00 2001 From: Omkaar <79257339+Pysics@users.noreply.github.com> Date: Fri, 13 May 2022 18:21:44 +0530 Subject: [PATCH] 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 --- ciphers/enigma_machine2.py | 6 +++--- ciphers/hill_cipher.py | 2 +- data_structures/hashing/number_theory/prime_numbers.py | 2 +- divide_and_conquer/strassen_matrix_multiplication.py | 2 +- dynamic_programming/rod_cutting.py | 2 +- hashes/enigma_machine.py | 2 +- maths/integration_by_simpson_approx.py | 10 +++------- matrix/matrix_operation.py | 2 +- project_euler/problem_067/sol1.py | 2 +- sorts/external_sort.py | 2 +- 10 files changed, 14 insertions(+), 18 deletions(-) diff --git a/ciphers/enigma_machine2.py b/ciphers/enigma_machine2.py index 9252dd0ed..70f84752d 100644 --- a/ciphers/enigma_machine2.py +++ b/ciphers/enigma_machine2.py @@ -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 diff --git a/ciphers/hill_cipher.py b/ciphers/hill_cipher.py index bc8f5b41b..d8e436e92 100644 --- a/ciphers/hill_cipher.py +++ b/ciphers/hill_cipher.py @@ -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: """ diff --git a/data_structures/hashing/number_theory/prime_numbers.py b/data_structures/hashing/number_theory/prime_numbers.py index db4d40f47..bf614e7d4 100644 --- a/data_structures/hashing/number_theory/prime_numbers.py +++ b/data_structures/hashing/number_theory/prime_numbers.py @@ -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): diff --git a/divide_and_conquer/strassen_matrix_multiplication.py b/divide_and_conquer/strassen_matrix_multiplication.py index ca10e04ab..17efcfc7c 100644 --- a/divide_and_conquer/strassen_matrix_multiplication.py +++ b/divide_and_conquer/strassen_matrix_multiplication.py @@ -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) diff --git a/dynamic_programming/rod_cutting.py b/dynamic_programming/rod_cutting.py index 442a39cb1..79104d8f4 100644 --- a/dynamic_programming/rod_cutting.py +++ b/dynamic_programming/rod_cutting.py @@ -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)}" ) diff --git a/hashes/enigma_machine.py b/hashes/enigma_machine.py index d1cb6efc2..b0d45718e 100644 --- a/hashes/enigma_machine.py +++ b/hashes/enigma_machine.py @@ -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!" ) diff --git a/maths/integration_by_simpson_approx.py b/maths/integration_by_simpson_approx.py index feb77440d..408041de9 100644 --- a/maths/integration_by_simpson_approx.py +++ b/maths/integration_by_simpson_approx.py @@ -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}" diff --git a/matrix/matrix_operation.py b/matrix/matrix_operation.py index 6d0cd4e65..8e5d0f583 100644 --- a/matrix/matrix_operation.py +++ b/matrix/matrix_operation.py @@ -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]) diff --git a/project_euler/problem_067/sol1.py b/project_euler/problem_067/sol1.py index ebfa865a9..527d4dc59 100644 --- a/project_euler/problem_067/sol1.py +++ b/project_euler/problem_067/sol1.py @@ -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])): diff --git a/sorts/external_sort.py b/sorts/external_sort.py index 060e67adf..7af7dc0a6 100644 --- a/sorts/external_sort.py +++ b/sorts/external_sort.py @@ -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: