diff --git a/matrix/matrix_operation.py b/matrix/matrix_operation.py index b5a724ad688c..42a94da12375 100644 --- a/matrix/matrix_operation.py +++ b/matrix/matrix_operation.py @@ -27,9 +27,11 @@ def subtract(matrix_a: List[list], matrix_b: List[list]) -> List[list]: >>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]]) [[-1, -0.5], [-1, -1.5]] """ - if _check_not_integer(matrix_a)\ - and _check_not_integer(matrix_b)\ - and _verify_matrix_sizes(matrix_a, matrix_b): + if ( + _check_not_integer(matrix_a) + and _check_not_integer(matrix_b) + and _verify_matrix_sizes(matrix_a, matrix_b) + ): return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)] @@ -60,8 +62,9 @@ def multiply(matrix_a: List[list], matrix_b: List[list]) -> List[list]: f"Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) " f"and ({rows[1]},{cols[1]})" ) - return [[sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)] - for i in matrix_a] + return [ + [sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)] for i in matrix_a + ] def identity(n: int) -> List[list]: @@ -95,8 +98,8 @@ def minor(matrix: List[list], row: int, column: int) -> List[list]: >>> minor([[1, 2], [3, 4]], 1, 1) [[1]] """ - minor = matrix[: row] + matrix[row + 1:] - return [row[:column] + row[column + 1:] for row in minor] + minor = matrix[:row] + matrix[row + 1 :] + return [row[:column] + row[column + 1 :] for row in minor] def determinant(matrix: List[list]) -> int: @@ -109,8 +112,10 @@ def determinant(matrix: List[list]) -> int: if len(matrix) == 1: return matrix[0][0] - return sum(x * determinant(minor(matrix, 0, i)) * (-1) ** i - for i, x in enumerate(matrix[0])) + return sum( + x * determinant(minor(matrix, 0, i)) * (-1) ** i + for i, x in enumerate(matrix[0]) + ) def inverse(matrix: List[list]) -> List[list]: @@ -124,9 +129,10 @@ def inverse(matrix: List[list]) -> List[list]: if det == 0: return None - matrix_minor = [[determinant(minor(matrix, i, j)) - for j in range(len(matrix))] - for i in range(len(matrix))] + matrix_minor = [ + [determinant(minor(matrix, i, j)) for j in range(len(matrix))] + for i in range(len(matrix)) + ] cofactors = [ [x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])] @@ -159,14 +165,10 @@ def _verify_matrix_sizes(matrix_a: List[list], matrix_b: List[list]) -> Tuple[li def main(): matrix_a = [[12, 10], [3, 9]] matrix_b = [[3, 4], [7, 4]] - matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], - [31, 32, 33, 34], [41, 42, 43, 44]] + matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]] matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]] - print( - f"Add Operation, {add(matrix_a, matrix_b) = } \n") - print( - f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n", - ) + print(f"Add Operation, {add(matrix_a, matrix_b) = } \n") + print(f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",) print(f"Identity: {identity(5)}\n") print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n") print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n") diff --git a/sorts/sleep_sort.py b/sorts/sleep_sort.py deleted file mode 100644 index 0feda9c5e038..000000000000 --- a/sorts/sleep_sort.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -Sleep sort is probably the wierdest of all sorting functions with time-complexity of -O(max(input)+n) which is quite different from almost all other sorting techniques. -If the number of inputs is small then the complexity can be approximated to be -O(max(input)) which is a constant - -If the number of inputs is large, the complexity is approximately O(n). - -This function uses multithreading a kind of higher order programming and calls n -functions, each with a sleep time equal to its number. Hence each of function wakes -in sorted time. - -This function is not stable for very large values. - -https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort -""" -from threading import Timer -from time import sleep -from typing import List - - -def sleep_sort(values: List[int]) -> List[int]: - """ - Sort the list using sleepsort. - >>> sleep_sort([3, 2, 4, 7, 3, 6, 9, 1]) - [1, 2, 3, 3, 4, 6, 7, 9] - >>> sleep_sort([3, 2, 1, 9, 8, 4, 2]) - [1, 2, 2, 3, 4, 8, 9] - """ - sleep_sort.result = [] - - def append_to_result(x): - sleep_sort.result.append(x) - - mx = values[0] - for value in values: - if mx < value: - mx = value - Timer(value, append_to_result, [value]).start() - sleep(mx + 1) - return sleep_sort.result - - -if __name__ == "__main__": - import doctest - - doctest.testmod() - - print(sleep_sort([3, 2, 4, 7, 3, 6, 9, 1]))