From 35c6c7e6871c73f0d630bda984b3599e2160debb Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 18:58:53 +0530 Subject: [PATCH 01/51] Add: Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 matrix/matrix_prefix_sum.py diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py new file mode 100644 index 000000000000..96dcec4558ec --- /dev/null +++ b/matrix/matrix_prefix_sum.py @@ -0,0 +1,85 @@ +# Python Program to find prefix sum of a 2D array + +from typing import List + +def calculate_prefix_sum(matrix: List[List[int]]) -> List[List[int]]: + """ + Calculate the prefix sum of a 2D matrix. + + Prefix sum is a technique used to efficiently calculate the cumulative sum of subarrays in a 2D array. + The idea is to compute a new array where each element represents the sum of all elements in the original array + up to that position. + + Prefix Sum Formula: + prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j] + + :param matrix: A 2D matrix. + :return: A matrix containing the prefix sums. + + >>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) + [[1, 2, 3], [2, 4, 6], [3, 6, 9]] + + >>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + [[1, 3, 6], [5, 12, 21], [12, 27, 45]] + """ + rows = len(matrix) + cols = len(matrix[0]) + + # Initialize the prefix sum matrix with zeros, with the same dimensions as the original matrix. + prefix_sum = [[0 for _ in range(cols)] for _ in range(rows)] + + # The prefix sum at the top-left corner is the same as the original matrix value. + prefix_sum[0][0] = matrix[0][0] + + # Calculate cumulative sums for the first row. + for i in range(1, cols): + # The value in the first row is the sum of the previous value in the same row + # and the value from the original matrix at the current column. + prefix_sum[0][i] = prefix_sum[0][i - 1] + matrix[0][i] + + # Calculate cumulative sums for the first column. + for i in range(1, rows): + # The value in the first column is the sum of the previous value in the same column + # and the value from the original matrix at the current row. + prefix_sum[i][0] = prefix_sum[i - 1][0] + matrix[i][0] + + # Update the values in the cells using the general formula. + for i in range(1, rows): + for j in range(1, cols): + + # The value in each cell is the sum of: + # - The cell above it (prefix_sum[i-1][j]) + # - The cell to the left of it (prefix_sum[i][j-1]) + # - Subtracting the overlapping cell (top-left: prefix_sum[i-1][j-1]) + # - Adding the value from the original matrix (matrix[i][j]) + prefix_sum[i][j] = ( + prefix_sum[i - 1][j] # Sum of values above + + prefix_sum[i][j - 1] # Sum of values to the left + - prefix_sum[i - 1][j - 1] # Subtract the overlapping cell + + matrix[i][j] # Add the value from the original matrix + ) + + return prefix_sum + +def display_matrix(matrix): + """ + Display a 2D matrix. + + :param matrix: A 2D matrix. + """ + for row in matrix: + # Join the elements of each row with spaces and print the result. + print(" ".join(str(cell) for cell in row)) + +if __name__ == "__main__": + a = [ + [1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1] + ] + # Calculate the prefix sum of the 2D matrix + prefix_sum_matrix = calculate_prefix_sum(a) + + # Display the prefix sum matrix + display_matrix(prefix_sum_matrix) From a8603eab30b63cd224cd9ad1895237fe53612736 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 13:42:09 +0000 Subject: [PATCH 02/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 96dcec4558ec..68d14c147cfd 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -2,6 +2,7 @@ from typing import List + def calculate_prefix_sum(matrix: List[List[int]]) -> List[List[int]]: """ Calculate the prefix sum of a 2D matrix. @@ -9,16 +10,16 @@ def calculate_prefix_sum(matrix: List[List[int]]) -> List[List[int]]: Prefix sum is a technique used to efficiently calculate the cumulative sum of subarrays in a 2D array. The idea is to compute a new array where each element represents the sum of all elements in the original array up to that position. - + Prefix Sum Formula: prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j] :param matrix: A 2D matrix. :return: A matrix containing the prefix sums. - + >>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) [[1, 2, 3], [2, 4, 6], [3, 6, 9]] - + >>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [[1, 3, 6], [5, 12, 21], [12, 27, 45]] """ @@ -27,7 +28,7 @@ def calculate_prefix_sum(matrix: List[List[int]]) -> List[List[int]]: # Initialize the prefix sum matrix with zeros, with the same dimensions as the original matrix. prefix_sum = [[0 for _ in range(cols)] for _ in range(rows)] - + # The prefix sum at the top-left corner is the same as the original matrix value. prefix_sum[0][0] = matrix[0][0] @@ -46,21 +47,21 @@ def calculate_prefix_sum(matrix: List[List[int]]) -> List[List[int]]: # Update the values in the cells using the general formula. for i in range(1, rows): for j in range(1, cols): - # The value in each cell is the sum of: # - The cell above it (prefix_sum[i-1][j]) # - The cell to the left of it (prefix_sum[i][j-1]) # - Subtracting the overlapping cell (top-left: prefix_sum[i-1][j-1]) # - Adding the value from the original matrix (matrix[i][j]) prefix_sum[i][j] = ( - prefix_sum[i - 1][j] # Sum of values above - + prefix_sum[i][j - 1] # Sum of values to the left - - prefix_sum[i - 1][j - 1] # Subtract the overlapping cell - + matrix[i][j] # Add the value from the original matrix + prefix_sum[i - 1][j] # Sum of values above + + prefix_sum[i][j - 1] # Sum of values to the left + - prefix_sum[i - 1][j - 1] # Subtract the overlapping cell + + matrix[i][j] # Add the value from the original matrix ) return prefix_sum + def display_matrix(matrix): """ Display a 2D matrix. @@ -71,13 +72,9 @@ def display_matrix(matrix): # Join the elements of each row with spaces and print the result. print(" ".join(str(cell) for cell in row)) + if __name__ == "__main__": - a = [ - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1] - ] + a = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(a) From b21b10d8aeb26606ba84afdbfc349b256e780c2f Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 19:16:10 +0530 Subject: [PATCH 03/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 96dcec4558ec..437a7bc13424 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,8 +1,6 @@ # Python Program to find prefix sum of a 2D array -from typing import List - -def calculate_prefix_sum(matrix: List[List[int]]) -> List[List[int]]: +def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. @@ -61,7 +59,7 @@ def calculate_prefix_sum(matrix: List[List[int]]) -> List[List[int]]: return prefix_sum -def display_matrix(matrix): +def display_matrix(matrix) -> None: """ Display a 2D matrix. @@ -72,14 +70,14 @@ def display_matrix(matrix): print(" ".join(str(cell) for cell in row)) if __name__ == "__main__": - a = [ + matrix = [ [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1] ] # Calculate the prefix sum of the 2D matrix - prefix_sum_matrix = calculate_prefix_sum(a) + prefix_sum_matrix = calculate_prefix_sum(matrix) # Display the prefix sum matrix display_matrix(prefix_sum_matrix) From 89d32bd82d0b823efd815e0e6d7d69afb3a1e287 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 13:47:36 +0000 Subject: [PATCH 04/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 9f719145641a..136dbe21c834 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,5 +1,6 @@ # Python Program to find prefix sum of a 2D array + def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. @@ -58,6 +59,7 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: return prefix_sum + def display_matrix(matrix): """ Display a 2D matrix. @@ -70,12 +72,7 @@ def display_matrix(matrix): if __name__ == "__main__": - a = [ - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1] - ] + a = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) From 2cee13cff04ecdbd05b589089436571936a4742d Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 19:18:41 +0530 Subject: [PATCH 05/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 9f719145641a..0e92fc8fbefe 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -58,7 +58,7 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: return prefix_sum -def display_matrix(matrix): +def display_matrix(matrix) -> None: """ Display a 2D matrix. From de886cfeace2dd642f9bc51fdd8cb8b7e684de31 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 13:50:50 +0000 Subject: [PATCH 06/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 2ddbd7a9605e..40bc198608f9 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -59,6 +59,7 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: return prefix_sum + def display_matrix(matrix) -> None: """ Display a 2D matrix. From 336adf289934040e10c9e0046f97ef58466cdc27 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 19:26:19 +0530 Subject: [PATCH 07/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 43 ++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 2ddbd7a9605e..10b88f4a7b4c 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,6 +1,5 @@ # Python Program to find prefix sum of a 2D array - def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. @@ -14,52 +13,42 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: :param matrix: A 2D matrix. :return: A matrix containing the prefix sums. - - >>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) - [[1, 2, 3], [2, 4, 6], [3, 6, 9]] - - >>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - [[1, 3, 6], [5, 12, 21], [12, 27, 45]] """ rows = len(matrix) cols = len(matrix[0]) # Initialize the prefix sum matrix with zeros, with the same dimensions as the original matrix. - prefix_sum = [[0 for _ in range(cols)] for _ in range(rows)] + prefix_sum = [[0 for _ in range(cols)] for _ in range(rows] - # The prefix sum at the top-left corner is the same as the original matrix value. + # Calculate the prefix sum for the top-left cell. prefix_sum[0][0] = matrix[0][0] # Calculate cumulative sums for the first row. for i in range(1, cols): - # The value in the first row is the sum of the previous value in the same row - # and the value from the original matrix at the current column. prefix_sum[0][i] = prefix_sum[0][i - 1] + matrix[0][i] # Calculate cumulative sums for the first column. for i in range(1, rows): - # The value in the first column is the sum of the previous value in the same column - # and the value from the original matrix at the current row. prefix_sum[i][0] = prefix_sum[i - 1][0] + matrix[i][0] # Update the values in the cells using the general formula. for i in range(1, rows): for j in range(1, cols): # The value in each cell is the sum of: - # - The cell above it (prefix_sum[i-1][j]) - # - The cell to the left of it (prefix_sum[i][j-1]) - # - Subtracting the overlapping cell (top-left: prefix_sum[i-1][j-1]) - # - Adding the value from the original matrix (matrix[i][j]) + # - The cell above it + # - The cell to the left of it + # - Subtracting the overlapping cell + # - Adding the value from the original matrix prefix_sum[i][j] = ( - prefix_sum[i - 1][j] # Sum of values above - + prefix_sum[i][j - 1] # Sum of values to the left - - prefix_sum[i - 1][j - 1] # Subtract the overlapping cell - + matrix[i][j] # Add the value from the original matrix + prefix_sum[i - 1][j] + + prefix_sum[i][j - 1] - + prefix_sum[i - 1][j - 1] + + matrix[i][j] ) return prefix_sum -def display_matrix(matrix) -> None: +def display_matrix(matrix: list[list[int]]) -> None: """ Display a 2D matrix. @@ -67,11 +56,15 @@ def display_matrix(matrix) -> None: """ for row in matrix: # Join the elements of each row with spaces and print the result. - print(" ".join(str(cell) for cell in row)) - + print(" ".join(map(str, row)) if __name__ == "__main__": - a = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] + matrix = [ + [1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1] + ] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) From b43f4751fcdae45858fd5911c6a1cd4359b113a9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 13:57:44 +0000 Subject: [PATCH 08/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 10b88f4a7b4c..ebf31d263977 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -60,8 +60,8 @@ def display_matrix(matrix: list[list[int]]) -> None: if __name__ == "__main__": matrix = [ - [1, 1, 1, 1], - [1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1] ] From 37718c7eafd8b2f79ad28fe8731dd4aadebfc392 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 19:30:44 +0530 Subject: [PATCH 09/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 10b88f4a7b4c..b4c26d602936 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -3,22 +3,27 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. - - Prefix sum is a technique used to efficiently calculate the cumulative sum of subarrays in a 2D array. - The idea is to compute a new array where each element represents the sum of all elements in the original array - up to that position. - Prefix Sum Formula: - prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j] + prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] + - prefix_sum[i - 1][j - 1] + matrix[i][j] :param matrix: A 2D matrix. :return: A matrix containing the prefix sums. + + :param matrix: A 2D matrix. + :return: A matrix containing the prefix sums. + + >>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) + [[1, 2, 3], [2, 4, 6], [3, 6, 9]] + + >>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + [[1, 3, 6], [5, 12, 21], [12, 27, 45]] """ rows = len(matrix) cols = len(matrix[0]) # Initialize the prefix sum matrix with zeros, with the same dimensions as the original matrix. - prefix_sum = [[0 for _ in range(cols)] for _ in range(rows] + prefix_sum = [[0 for _ in range(cols)] for _ in range(rows)] # Calculate the prefix sum for the top-left cell. prefix_sum[0][0] = matrix[0][0] @@ -56,7 +61,7 @@ def display_matrix(matrix: list[list[int]]) -> None: """ for row in matrix: # Join the elements of each row with spaces and print the result. - print(" ".join(map(str, row)) + print(" ".join(map(str, row))) if __name__ == "__main__": matrix = [ From e9403b2df71167d8f890e6d85f855f2ae13d7a37 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 19:31:14 +0530 Subject: [PATCH 10/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index c8ff664c6562..b4c26d602936 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -65,8 +65,8 @@ def display_matrix(matrix: list[list[int]]) -> None: if __name__ == "__main__": matrix = [ - [1, 1, 1, 1], - [1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1] ] From 7d7e2e78abbddbf3f9811aaf61d65cc76ffa755f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:01:53 +0000 Subject: [PATCH 11/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index b4c26d602936..751041293072 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,21 +1,22 @@ # Python Program to find prefix sum of a 2D array + def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. Prefix Sum Formula: - prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] + prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j] :param matrix: A 2D matrix. :return: A matrix containing the prefix sums. - + :param matrix: A 2D matrix. :return: A matrix containing the prefix sums. - + >>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) [[1, 2, 3], [2, 4, 6], [3, 6, 9]] - + >>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [[1, 3, 6], [5, 12, 21], [12, 27, 45]] """ @@ -45,14 +46,15 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: # - Subtracting the overlapping cell # - Adding the value from the original matrix prefix_sum[i][j] = ( - prefix_sum[i - 1][j] + - prefix_sum[i][j - 1] - - prefix_sum[i - 1][j - 1] + - matrix[i][j] + prefix_sum[i - 1][j] + + prefix_sum[i][j - 1] + - prefix_sum[i - 1][j - 1] + + matrix[i][j] ) return prefix_sum + def display_matrix(matrix: list[list[int]]) -> None: """ Display a 2D matrix. @@ -63,13 +65,9 @@ def display_matrix(matrix: list[list[int]]) -> None: # Join the elements of each row with spaces and print the result. print(" ".join(map(str, row))) + if __name__ == "__main__": - matrix = [ - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1] - ] + matrix = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) From f9878233a1a7b4a0b300e04c2536dd49850fe9fb Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 19:36:10 +0530 Subject: [PATCH 12/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index b4c26d602936..e209855b5fe5 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -10,9 +10,6 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: :param matrix: A 2D matrix. :return: A matrix containing the prefix sums. - :param matrix: A 2D matrix. - :return: A matrix containing the prefix sums. - >>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) [[1, 2, 3], [2, 4, 6], [3, 6, 9]] From 4ed2d72599f858eca5cb0a76801b64c8be245af6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:12:37 +0000 Subject: [PATCH 13/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index e209855b5fe5..ae2e68d74de0 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,18 +1,19 @@ # Python Program to find prefix sum of a 2D array + def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. Prefix Sum Formula: - prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] + prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j] :param matrix: A 2D matrix. :return: A matrix containing the prefix sums. - + >>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) [[1, 2, 3], [2, 4, 6], [3, 6, 9]] - + >>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [[1, 3, 6], [5, 12, 21], [12, 27, 45]] """ @@ -42,14 +43,15 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: # - Subtracting the overlapping cell # - Adding the value from the original matrix prefix_sum[i][j] = ( - prefix_sum[i - 1][j] + - prefix_sum[i][j - 1] - - prefix_sum[i - 1][j - 1] + - matrix[i][j] + prefix_sum[i - 1][j] + + prefix_sum[i][j - 1] + - prefix_sum[i - 1][j - 1] + + matrix[i][j] ) return prefix_sum + def display_matrix(matrix: list[list[int]]) -> None: """ Display a 2D matrix. @@ -60,13 +62,9 @@ def display_matrix(matrix: list[list[int]]) -> None: # Join the elements of each row with spaces and print the result. print(" ".join(map(str, row))) + if __name__ == "__main__": - matrix = [ - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1] - ] + matrix = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) From 157c7440517d1391bf64c398c974bc623e15a5b5 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 19:52:37 +0530 Subject: [PATCH 14/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index e209855b5fe5..31cd568fb7fa 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -5,14 +5,14 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: Calculate the prefix sum of a 2D matrix. Prefix Sum Formula: prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - - prefix_sum[i - 1][j - 1] + matrix[i][j] + - prefix_sum[i - 1][j - 1] + matrix[i][j] :param matrix: A 2D matrix. :return: A matrix containing the prefix sums. - + >>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) [[1, 2, 3], [2, 4, 6], [3, 6, 9]] - + >>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [[1, 3, 6], [5, 12, 21], [12, 27, 45]] """ From 5c3a01f84b3ea7e3850eca7ad80013edeae22d02 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:24:45 +0000 Subject: [PATCH 15/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 31cd568fb7fa..c986a37d5627 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,10 +1,11 @@ # Python Program to find prefix sum of a 2D array + def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. Prefix Sum Formula: - prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] + prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j] :param matrix: A 2D matrix. @@ -42,14 +43,15 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: # - Subtracting the overlapping cell # - Adding the value from the original matrix prefix_sum[i][j] = ( - prefix_sum[i - 1][j] + - prefix_sum[i][j - 1] - - prefix_sum[i - 1][j - 1] + - matrix[i][j] + prefix_sum[i - 1][j] + + prefix_sum[i][j - 1] + - prefix_sum[i - 1][j - 1] + + matrix[i][j] ) return prefix_sum + def display_matrix(matrix: list[list[int]]) -> None: """ Display a 2D matrix. @@ -60,13 +62,9 @@ def display_matrix(matrix: list[list[int]]) -> None: # Join the elements of each row with spaces and print the result. print(" ".join(map(str, row))) + if __name__ == "__main__": - matrix = [ - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1] - ] + matrix = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) From 6b718c526820a51d1509acf6ef932175a7100f22 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 19:57:18 +0530 Subject: [PATCH 16/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 31cd568fb7fa..4500c1b4a1e4 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -19,7 +19,8 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: rows = len(matrix) cols = len(matrix[0]) - # Initialize the prefix sum matrix with zeros, with the same dimensions as the original matrix. + # Initialize the prefix sum matrix with zeros, with the + # same dimensions as the original matrix. prefix_sum = [[0 for _ in range(cols)] for _ in range(rows)] # Calculate the prefix sum for the top-left cell. From 11aa2139d86ccc86f32b1a97f316687f9bd88bc5 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 19:57:36 +0530 Subject: [PATCH 17/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 2d0f81285504..4500c1b4a1e4 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,11 +1,10 @@ # Python Program to find prefix sum of a 2D array - def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. Prefix Sum Formula: - prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] + prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j] :param matrix: A 2D matrix. @@ -44,15 +43,14 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: # - Subtracting the overlapping cell # - Adding the value from the original matrix prefix_sum[i][j] = ( - prefix_sum[i - 1][j] - + prefix_sum[i][j - 1] - - prefix_sum[i - 1][j - 1] - + matrix[i][j] + prefix_sum[i - 1][j] + + prefix_sum[i][j - 1] - + prefix_sum[i - 1][j - 1] + + matrix[i][j] ) return prefix_sum - def display_matrix(matrix: list[list[int]]) -> None: """ Display a 2D matrix. @@ -63,9 +61,13 @@ def display_matrix(matrix: list[list[int]]) -> None: # Join the elements of each row with spaces and print the result. print(" ".join(map(str, row))) - if __name__ == "__main__": - matrix = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] + matrix = [ + [1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1] + ] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) From 3ab1ea654b402dbfb10f67bfcd3735c08bed71b5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:29:42 +0000 Subject: [PATCH 18/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 4500c1b4a1e4..2d0f81285504 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,10 +1,11 @@ # Python Program to find prefix sum of a 2D array + def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. Prefix Sum Formula: - prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] + prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j] :param matrix: A 2D matrix. @@ -43,14 +44,15 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: # - Subtracting the overlapping cell # - Adding the value from the original matrix prefix_sum[i][j] = ( - prefix_sum[i - 1][j] + - prefix_sum[i][j - 1] - - prefix_sum[i - 1][j - 1] + - matrix[i][j] + prefix_sum[i - 1][j] + + prefix_sum[i][j - 1] + - prefix_sum[i - 1][j - 1] + + matrix[i][j] ) return prefix_sum + def display_matrix(matrix: list[list[int]]) -> None: """ Display a 2D matrix. @@ -61,13 +63,9 @@ def display_matrix(matrix: list[list[int]]) -> None: # Join the elements of each row with spaces and print the result. print(" ".join(map(str, row))) + if __name__ == "__main__": - matrix = [ - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1] - ] + matrix = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) From 97d14143e9d869cee9999e23e8d27f35047e7fcd Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 20:03:40 +0530 Subject: [PATCH 19/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 4500c1b4a1e4..7a15927e4b4d 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -4,7 +4,7 @@ def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. Prefix Sum Formula: - prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] + prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j] :param matrix: A 2D matrix. @@ -63,13 +63,13 @@ def display_matrix(matrix: list[list[int]]) -> None: if __name__ == "__main__": matrix = [ - [1, 1, 1, 1], - [1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1] ] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) - + # Display the prefix sum matrix display_matrix(prefix_sum_matrix) From 0853c35f897bd9181e4083fffd12f8c343a607a0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:35:02 +0000 Subject: [PATCH 20/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 2bd27a450a63..2d0f81285504 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -65,14 +65,9 @@ def display_matrix(matrix: list[list[int]]) -> None: if __name__ == "__main__": - matrix = [ - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1] - ] + matrix = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) - + # Display the prefix sum matrix display_matrix(prefix_sum_matrix) From b01f63749d23ff34dfcd0e7d7b3b8c1160cf0719 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 20:06:14 +0530 Subject: [PATCH 21/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 2bd27a450a63..a638ec4c2f41 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,6 +1,5 @@ # Python Program to find prefix sum of a 2D array - def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. @@ -73,6 +72,6 @@ def display_matrix(matrix: list[list[int]]) -> None: ] # Calculate the prefix sum of the 2D matrix prefix_sum_matrix = calculate_prefix_sum(matrix) - + # Display the prefix sum matrix - display_matrix(prefix_sum_matrix) + display_matrix(prefix_sum_matrix) \ No newline at end of file From 778df8702d70cbacced0324616a2798a0581b787 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:37:21 +0000 Subject: [PATCH 22/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 389d8bc44292..2d0f81285504 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,5 +1,6 @@ # Python Program to find prefix sum of a 2D array + def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. @@ -69,4 +70,4 @@ def display_matrix(matrix: list[list[int]]) -> None: prefix_sum_matrix = calculate_prefix_sum(matrix) # Display the prefix sum matrix - display_matrix(prefix_sum_matrix) \ No newline at end of file + display_matrix(prefix_sum_matrix) From b568f9d0ab0ca7473e03b6a10dac3b1064fd38ec Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 20:07:23 +0530 Subject: [PATCH 23/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 389d8bc44292..5f6284527df5 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -69,4 +69,4 @@ def display_matrix(matrix: list[list[int]]) -> None: prefix_sum_matrix = calculate_prefix_sum(matrix) # Display the prefix sum matrix - display_matrix(prefix_sum_matrix) \ No newline at end of file + display_matrix(prefix_sum_matrix) From aba2ef288defd59acf56a73a9028e37184a426d0 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 20:07:44 +0530 Subject: [PATCH 24/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 1 - 1 file changed, 1 deletion(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 2d0f81285504..5f6284527df5 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,6 +1,5 @@ # Python Program to find prefix sum of a 2D array - def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. From 6280be47f0ad78b35f07dfc88f359a72a64d7347 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:38:42 +0000 Subject: [PATCH 25/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 5f6284527df5..2d0f81285504 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,5 +1,6 @@ # Python Program to find prefix sum of a 2D array + def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. From 896f3a96479e3bbac539ec4c40cf829652f93f90 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 20:13:28 +0530 Subject: [PATCH 26/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 5f6284527df5..cd699ecf1260 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -1,4 +1,6 @@ -# Python Program to find prefix sum of a 2D array +""" +Python Program to find prefix sum of a 2D array +""" def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ From 01e6e1c76bb22d8851dfce7e4158cc4e3e8a4fbf Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 20:13:52 +0530 Subject: [PATCH 27/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 1 - 1 file changed, 1 deletion(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index 3e4215811c34..cd699ecf1260 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -2,7 +2,6 @@ Python Program to find prefix sum of a 2D array """ - def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. From 27022eb5287ed9e40bb29e5cf535b872793768de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:44:35 +0000 Subject: [PATCH 28/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index cd699ecf1260..3e4215811c34 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -2,6 +2,7 @@ Python Program to find prefix sum of a 2D array """ + def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: """ Calculate the prefix sum of a 2D matrix. From 0d8007d9d58092ad127c6cf9011c0070ae3639fe Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 20:20:05 +0530 Subject: [PATCH 29/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index cd699ecf1260..a3f67c238f33 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -59,6 +59,17 @@ def display_matrix(matrix: list[list[int]]) -> None: Display a 2D matrix. :param matrix: A 2D matrix. + + Display a 2D matrix by printing each row's elements separated by spaces. + + >>> display_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + 1 2 3 + 4 5 6 + 7 8 9 + + >>> display_matrix([[10, 20, 30], [40, 50, 60]]) + 10 20 30 + 40 50 60 """ for row in matrix: # Join the elements of each row with spaces and print the result. From fbc4fa71a9ec949ed6ced817b39e32ea7329e1a6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:51:02 +0000 Subject: [PATCH 30/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/matrix_prefix_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index d591306cb3ac..ada9785b7554 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -60,7 +60,7 @@ def display_matrix(matrix: list[list[int]]) -> None: Display a 2D matrix. :param matrix: A 2D matrix. - + Display a 2D matrix by printing each row's elements separated by spaces. >>> display_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) From b9311e95264aff2a01952d41618f142766131364 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 20:21:44 +0530 Subject: [PATCH 31/51] Changes made in Matrix Prefix Sum --- matrix/matrix_prefix_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py index d591306cb3ac..ada9785b7554 100644 --- a/matrix/matrix_prefix_sum.py +++ b/matrix/matrix_prefix_sum.py @@ -60,7 +60,7 @@ def display_matrix(matrix: list[list[int]]) -> None: Display a 2D matrix. :param matrix: A 2D matrix. - + Display a 2D matrix by printing each row's elements separated by spaces. >>> display_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) From 9da4957d03e2d7aa9d6566b8a35e6f2821721dee Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 21:15:18 +0530 Subject: [PATCH 32/51] Add: Distinct Subsequences --- dynamic_programming/distinct_sequences.py | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 dynamic_programming/distinct_sequences.py diff --git a/dynamic_programming/distinct_sequences.py b/dynamic_programming/distinct_sequences.py new file mode 100644 index 000000000000..caad3689610d --- /dev/null +++ b/dynamic_programming/distinct_sequences.py @@ -0,0 +1,73 @@ +""" +author: Aayush Soni +Given two strings s and t, return the number of distinct subsequences of s which equals t. +Input: s = "babgbag", t = "bag" +Output: 5 +Explanation: As shown below, there are 5 ways you can generate "bag" from s. +>>> "babgbag" +>>> "babgbag" +>>> "babgbag" +>>> "babgbag" +>>> "babgbag" +Leetcode link: https://leetcode.com/problems/distinct-subsequences/description/ +""" + +def countDistinctSubsequences(s: str, t: str) -> int: + + """ + This function calculates the number of distinct subsequences of s that equal t. + + :param s: The input string s. + :param t: The target string t. + :return: The number of distinct subsequences of s that equal t. + + >>> countDistinctSubsequences("babgbag", "bag") + 5 + >>> countDistinctSubsequences("rabbbit", "rabbit") + 3 + """ + + m = len(t) # Length of t + n = len(s) # Length of s + + # If t is longer than s, it can't be a subsequence. + if m > n: + return 0 + + # Create a matrix to store counts of subsequences. + # dp[i][j] stores the count of occurrences of t(1..i) in s(1..j). + dp = [[0 for _ in range(n + 1)] for __ in range(m + 1)] + + # Initialize the first column with all 0s. + # An empty string can't have another string as a subsequence. + for i in range(1, m + 1): + dp[i][0] = 0 + + # Initialize the first row with all 1s. + # An empty string is a subsequence of all strings. + for j in range(n + 1): + dp[0][j] = 1 + + # Fill dp[][] in a bottom-up manner + for i in range(1, m + 1): + for j in range(1, n + 1): + + # If the last characters don't match, + # the value is the same as the value without the last character in s. + if t[i - 1] != s[j - 1]: + dp[i][j] = dp[i][j - 1] + + # If the last characters match, + # the value is obtained by considering two cases: + # a) All subsequences without the last character in s. + # b) All subsequences without the last characters in both s and t. + else: + dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1] + + return dp[m][n] + +if __name__ == "__main__": + s = "babgbag" + t = "bag" + result = countDistinctSubsequences(s, t) + print(f"The number of distinct subsequences of '{t}' in '{s}' is {result}.") From b2f0756030c6e7d28874e0399c2e9f52f2e3b3de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:46:05 +0000 Subject: [PATCH 33/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/distinct_sequences.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/distinct_sequences.py b/dynamic_programming/distinct_sequences.py index caad3689610d..6f55feadd540 100644 --- a/dynamic_programming/distinct_sequences.py +++ b/dynamic_programming/distinct_sequences.py @@ -12,8 +12,8 @@ Leetcode link: https://leetcode.com/problems/distinct-subsequences/description/ """ -def countDistinctSubsequences(s: str, t: str) -> int: +def countDistinctSubsequences(s: str, t: str) -> int: """ This function calculates the number of distinct subsequences of s that equal t. @@ -51,7 +51,6 @@ def countDistinctSubsequences(s: str, t: str) -> int: # Fill dp[][] in a bottom-up manner for i in range(1, m + 1): for j in range(1, n + 1): - # If the last characters don't match, # the value is the same as the value without the last character in s. if t[i - 1] != s[j - 1]: @@ -66,6 +65,7 @@ def countDistinctSubsequences(s: str, t: str) -> int: return dp[m][n] + if __name__ == "__main__": s = "babgbag" t = "bag" From 61a28240a3d4d916ee2f381a9e8ae5b7a046888c Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 21:17:50 +0530 Subject: [PATCH 34/51] Changes made in Distinct Subsequences --- dynamic_programming/distinct_sequences.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/distinct_sequences.py b/dynamic_programming/distinct_sequences.py index caad3689610d..784a23ff3ee1 100644 --- a/dynamic_programming/distinct_sequences.py +++ b/dynamic_programming/distinct_sequences.py @@ -12,7 +12,7 @@ Leetcode link: https://leetcode.com/problems/distinct-subsequences/description/ """ -def countDistinctSubsequences(s: str, t: str) -> int: +def count_distinct_subsequences(s: str, t: str) -> int: """ This function calculates the number of distinct subsequences of s that equal t. From 3329261af80f02e8282394668131335848417a26 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:49:59 +0000 Subject: [PATCH 35/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/distinct_sequences.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dynamic_programming/distinct_sequences.py b/dynamic_programming/distinct_sequences.py index 99cdb738167c..d05a4092bd1b 100644 --- a/dynamic_programming/distinct_sequences.py +++ b/dynamic_programming/distinct_sequences.py @@ -12,6 +12,7 @@ Leetcode link: https://leetcode.com/problems/distinct-subsequences/description/ """ + def count_distinct_subsequences(s: str, t: str) -> int: """ This function calculates the number of distinct subsequences of s that equal t. From fb935b2aff5133cceb7526d8c2f8bedd4bf6bb84 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 21:21:17 +0530 Subject: [PATCH 36/51] Changes made in Distinct Subsequences --- dynamic_programming/distinct_sequences.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/distinct_sequences.py b/dynamic_programming/distinct_sequences.py index 99cdb738167c..03a29ecb86ab 100644 --- a/dynamic_programming/distinct_sequences.py +++ b/dynamic_programming/distinct_sequences.py @@ -14,7 +14,8 @@ def count_distinct_subsequences(s: str, t: str) -> int: """ - This function calculates the number of distinct subsequences of s that equal t. + This function calculates the number of distinct + subsequences of s that equal t. :param s: The input string s. :param t: The target string t. @@ -68,5 +69,5 @@ def count_distinct_subsequences(s: str, t: str) -> int: if __name__ == "__main__": s = "babgbag" t = "bag" - result = countDistinctSubsequences(s, t) + result = count_distinct_subsequences(s, t) print(f"The number of distinct subsequences of '{t}' in '{s}' is {result}.") From aaf1c3b9ac8ab4379474229122483e1d528621b0 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 21:22:19 +0530 Subject: [PATCH 37/51] Changes made in Distinct Subsequences --- dynamic_programming/distinct_sequences.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dynamic_programming/distinct_sequences.py b/dynamic_programming/distinct_sequences.py index 956a13ae0ed3..03a29ecb86ab 100644 --- a/dynamic_programming/distinct_sequences.py +++ b/dynamic_programming/distinct_sequences.py @@ -12,7 +12,6 @@ Leetcode link: https://leetcode.com/problems/distinct-subsequences/description/ """ - def count_distinct_subsequences(s: str, t: str) -> int: """ This function calculates the number of distinct From e268227404fed72cbe0d3cfd99849a76a0b3ae35 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 21:25:24 +0530 Subject: [PATCH 38/51] Changes made in Distinct Subsequences --- dynamic_programming/distinct_sequences.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/distinct_sequences.py b/dynamic_programming/distinct_sequences.py index 03a29ecb86ab..fc1fec381e95 100644 --- a/dynamic_programming/distinct_sequences.py +++ b/dynamic_programming/distinct_sequences.py @@ -1,6 +1,7 @@ """ author: Aayush Soni -Given two strings s and t, return the number of distinct subsequences of s which equals t. +Given two strings s and t, return the number of distinct subsequences +of s which equals t. Input: s = "babgbag", t = "bag" Output: 5 Explanation: As shown below, there are 5 ways you can generate "bag" from s. From 6ce845a5f07608bfa0ec281b8de0a7cc9892f7a0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:55:59 +0000 Subject: [PATCH 39/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/distinct_sequences.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dynamic_programming/distinct_sequences.py b/dynamic_programming/distinct_sequences.py index fc1fec381e95..c603f575d837 100644 --- a/dynamic_programming/distinct_sequences.py +++ b/dynamic_programming/distinct_sequences.py @@ -13,6 +13,7 @@ Leetcode link: https://leetcode.com/problems/distinct-subsequences/description/ """ + def count_distinct_subsequences(s: str, t: str) -> int: """ This function calculates the number of distinct From 7cb9ca22d726064089ff79e6d1730f9c9d0214ab Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 23 Oct 2023 21:31:12 +0530 Subject: [PATCH 40/51] Removed Distinct Subsequences --- dynamic_programming/distinct_sequences.py | 74 ----------------------- 1 file changed, 74 deletions(-) delete mode 100644 dynamic_programming/distinct_sequences.py diff --git a/dynamic_programming/distinct_sequences.py b/dynamic_programming/distinct_sequences.py deleted file mode 100644 index fc1fec381e95..000000000000 --- a/dynamic_programming/distinct_sequences.py +++ /dev/null @@ -1,74 +0,0 @@ -""" -author: Aayush Soni -Given two strings s and t, return the number of distinct subsequences -of s which equals t. -Input: s = "babgbag", t = "bag" -Output: 5 -Explanation: As shown below, there are 5 ways you can generate "bag" from s. ->>> "babgbag" ->>> "babgbag" ->>> "babgbag" ->>> "babgbag" ->>> "babgbag" -Leetcode link: https://leetcode.com/problems/distinct-subsequences/description/ -""" - -def count_distinct_subsequences(s: str, t: str) -> int: - """ - This function calculates the number of distinct - subsequences of s that equal t. - - :param s: The input string s. - :param t: The target string t. - :return: The number of distinct subsequences of s that equal t. - - >>> countDistinctSubsequences("babgbag", "bag") - 5 - >>> countDistinctSubsequences("rabbbit", "rabbit") - 3 - """ - - m = len(t) # Length of t - n = len(s) # Length of s - - # If t is longer than s, it can't be a subsequence. - if m > n: - return 0 - - # Create a matrix to store counts of subsequences. - # dp[i][j] stores the count of occurrences of t(1..i) in s(1..j). - dp = [[0 for _ in range(n + 1)] for __ in range(m + 1)] - - # Initialize the first column with all 0s. - # An empty string can't have another string as a subsequence. - for i in range(1, m + 1): - dp[i][0] = 0 - - # Initialize the first row with all 1s. - # An empty string is a subsequence of all strings. - for j in range(n + 1): - dp[0][j] = 1 - - # Fill dp[][] in a bottom-up manner - for i in range(1, m + 1): - for j in range(1, n + 1): - # If the last characters don't match, - # the value is the same as the value without the last character in s. - if t[i - 1] != s[j - 1]: - dp[i][j] = dp[i][j - 1] - - # If the last characters match, - # the value is obtained by considering two cases: - # a) All subsequences without the last character in s. - # b) All subsequences without the last characters in both s and t. - else: - dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1] - - return dp[m][n] - - -if __name__ == "__main__": - s = "babgbag" - t = "bag" - result = count_distinct_subsequences(s, t) - print(f"The number of distinct subsequences of '{t}' in '{s}' is {result}.") From e01c8635cbcf1c50222b9efa69457b4664f64ec1 Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 24 Oct 2023 19:42:23 +0530 Subject: [PATCH 41/51] Add: Generate Parentheses --- backtracking/generate_parentheses.py | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 backtracking/generate_parentheses.py diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py new file mode 100644 index 000000000000..994b5e54e0a8 --- /dev/null +++ b/backtracking/generate_parentheses.py @@ -0,0 +1,51 @@ +""" +author: Aayush Soni +Given n pairs of parentheses, write a function to generate all +combinations of well-formed parentheses. +Input: n = 2 +Output: ["(())","()()"] +Leetcode link: https://leetcode.com/problems/generate-parentheses/description/ +""" + + +def generateParenthesis(n: int) -> list[str]: + """ + Generate valid combinations of balanced parentheses for a given n. + + :param n: An integer representing the number of pairs of parentheses. + :return: A list of strings with valid combinations. + + This function uses a recursive approach to generate the combinations. + + Time Complexity: O(2^(2n)) - In the worst case, we have 2^(2n) combinations. + Space Complexity: O(2n) - Space used for recursion, where 'n' is the number of pairs. + + Example 1: + >>> generateParenthesis(3) + ['((()))', '(()())', '(())()', '()(())', '()()()'] + + Example 2: + >>> generateParenthesis(1) + ['()'] + """ + + def backtrack(partial, open_count, close_count): + if len(partial) == 2 * n: + result.append(partial) + return + + if open_count < n: + backtrack(partial + "(", open_count + 1, close_count) + + if close_count < open_count: + backtrack(partial + ")", open_count, close_count + 1) + + result = [] + backtrack("", 0, 0) + return result + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From c9d1d70c27c0b8e96c12d2acf5c4e99f8a128344 Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 24 Oct 2023 19:47:39 +0530 Subject: [PATCH 42/51] Add: Generate Parentheses --- backtracking/generate_parentheses.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py index 994b5e54e0a8..008a8b2a61f7 100644 --- a/backtracking/generate_parentheses.py +++ b/backtracking/generate_parentheses.py @@ -7,8 +7,7 @@ Leetcode link: https://leetcode.com/problems/generate-parentheses/description/ """ - -def generateParenthesis(n: int) -> list[str]: +def generate_parenthesis(n: int) -> list[str]: """ Generate valid combinations of balanced parentheses for a given n. @@ -21,15 +20,23 @@ def generateParenthesis(n: int) -> list[str]: Space Complexity: O(2n) - Space used for recursion, where 'n' is the number of pairs. Example 1: - >>> generateParenthesis(3) + >>> generate_parenthesis(3) ['((()))', '(()())', '(())()', '()(())', '()()()'] Example 2: - >>> generateParenthesis(1) + >>> generate_parenthesis(1) ['()'] """ - def backtrack(partial, open_count, close_count): + def backtrack(partial: str, open_count: int, close_count: int) -> None: + """ + Generate valid combinations of balanced parentheses. + + :param partial: A string representing the current combination. + :param open_count: An integer representing the count of open parentheses. + :param close_count: An integer representing the count of close parentheses. + :return: None + """ if len(partial) == 2 * n: result.append(partial) return @@ -44,8 +51,6 @@ def backtrack(partial, open_count, close_count): backtrack("", 0, 0) return result - if __name__ == "__main__": import doctest - doctest.testmod() From f037e2e1cd7211f902a191ee5aed396c45df32c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:18:45 +0000 Subject: [PATCH 43/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/generate_parentheses.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py index 008a8b2a61f7..a8ab814cc711 100644 --- a/backtracking/generate_parentheses.py +++ b/backtracking/generate_parentheses.py @@ -7,6 +7,7 @@ Leetcode link: https://leetcode.com/problems/generate-parentheses/description/ """ + def generate_parenthesis(n: int) -> list[str]: """ Generate valid combinations of balanced parentheses for a given n. @@ -51,6 +52,8 @@ def backtrack(partial: str, open_count: int, close_count: int) -> None: backtrack("", 0, 0) return result + if __name__ == "__main__": import doctest + doctest.testmod() From e9ac8f6effd147f70089cafe10527277fc2405ee Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 24 Oct 2023 19:50:28 +0530 Subject: [PATCH 44/51] Add: Generate Parentheses --- backtracking/generate_parentheses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py index 008a8b2a61f7..fdc6e92b83cb 100644 --- a/backtracking/generate_parentheses.py +++ b/backtracking/generate_parentheses.py @@ -17,7 +17,7 @@ def generate_parenthesis(n: int) -> list[str]: This function uses a recursive approach to generate the combinations. Time Complexity: O(2^(2n)) - In the worst case, we have 2^(2n) combinations. - Space Complexity: O(2n) - Space used for recursion, where 'n' is the number of pairs. + Space Complexity: O(n) - Space used for recursion, where 'n' is the number of pairs. Example 1: >>> generate_parenthesis(3) From 26643c19d7931478f7fd737f91f155abd02929cb Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 24 Oct 2023 19:51:02 +0530 Subject: [PATCH 45/51] Add: Generate Parentheses --- backtracking/generate_parentheses.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py index e6e449fbdd89..fdc6e92b83cb 100644 --- a/backtracking/generate_parentheses.py +++ b/backtracking/generate_parentheses.py @@ -7,7 +7,6 @@ Leetcode link: https://leetcode.com/problems/generate-parentheses/description/ """ - def generate_parenthesis(n: int) -> list[str]: """ Generate valid combinations of balanced parentheses for a given n. @@ -52,8 +51,6 @@ def backtrack(partial: str, open_count: int, close_count: int) -> None: backtrack("", 0, 0) return result - if __name__ == "__main__": import doctest - doctest.testmod() From c93ee0ce1f51e496e5b3688a109efb4b5188fc1a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:22:04 +0000 Subject: [PATCH 46/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/generate_parentheses.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py index fdc6e92b83cb..e6e449fbdd89 100644 --- a/backtracking/generate_parentheses.py +++ b/backtracking/generate_parentheses.py @@ -7,6 +7,7 @@ Leetcode link: https://leetcode.com/problems/generate-parentheses/description/ """ + def generate_parenthesis(n: int) -> list[str]: """ Generate valid combinations of balanced parentheses for a given n. @@ -51,6 +52,8 @@ def backtrack(partial: str, open_count: int, close_count: int) -> None: backtrack("", 0, 0) return result + if __name__ == "__main__": import doctest + doctest.testmod() From a1c36583af33f797bfc1cfc893b064331595dd64 Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 24 Oct 2023 19:55:46 +0530 Subject: [PATCH 47/51] Add: Generate Parentheses --- backtracking/generate_parentheses.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py index fdc6e92b83cb..e6e449fbdd89 100644 --- a/backtracking/generate_parentheses.py +++ b/backtracking/generate_parentheses.py @@ -7,6 +7,7 @@ Leetcode link: https://leetcode.com/problems/generate-parentheses/description/ """ + def generate_parenthesis(n: int) -> list[str]: """ Generate valid combinations of balanced parentheses for a given n. @@ -51,6 +52,8 @@ def backtrack(partial: str, open_count: int, close_count: int) -> None: backtrack("", 0, 0) return result + if __name__ == "__main__": import doctest + doctest.testmod() From 404220205184e6a08ff15c07e2bc6355ed337d5c Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 24 Oct 2023 20:49:01 +0530 Subject: [PATCH 48/51] Add: Generate Parentheses --- backtracking/generate_parentheses.py | 62 ++++++++++++++++++---------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py index e6e449fbdd89..05b4e85cbfd8 100644 --- a/backtracking/generate_parentheses.py +++ b/backtracking/generate_parentheses.py @@ -8,6 +8,45 @@ """ +def backtrack( + partial: str, open_count: int, close_count: int, n: int, result: list[str] +) -> None: + """ + Generate valid combinations of balanced parentheses using recursion. + + :param partial: A string representing the current combination. + :param open_count: An integer representing the count of open parentheses. + :param close_count: An integer representing the count of close parentheses. + :param n: An integer representing the total number of pairs. + :param result: A list to store valid combinations. + :return: None + + This function uses recursion to explore all possible combinations, + ensuring that at each step, the parentheses remain balanced. + + Example: + >>> result = [] + >>> backtrack("", 0, 0, 2, result) + >>> result + ['(())', '()()'] + """ + if len(partial) == 2 * n: + # When the combination is complete, add it to the result. + result.append( + partial + ) + return + + if open_count < n: + # If we can add an open parenthesis, do so, and recurse. + backtrack(partial + "(", open_count + 1, close_count, n, result) + + if close_count < open_count: + # If we can add a close parenthesis (it won't make the combination invalid), + # do so, and recurse. + backtrack(partial + ")", open_count, close_count + 1, n, result) + + def generate_parenthesis(n: int) -> list[str]: """ Generate valid combinations of balanced parentheses for a given n. @@ -18,7 +57,7 @@ def generate_parenthesis(n: int) -> list[str]: This function uses a recursive approach to generate the combinations. Time Complexity: O(2^(2n)) - In the worst case, we have 2^(2n) combinations. - Space Complexity: O(n) - Space used for recursion, where 'n' is the number of pairs. + Space Complexity: O(n) - where 'n' is the number of pairs. Example 1: >>> generate_parenthesis(3) @@ -29,27 +68,8 @@ def generate_parenthesis(n: int) -> list[str]: ['()'] """ - def backtrack(partial: str, open_count: int, close_count: int) -> None: - """ - Generate valid combinations of balanced parentheses. - - :param partial: A string representing the current combination. - :param open_count: An integer representing the count of open parentheses. - :param close_count: An integer representing the count of close parentheses. - :return: None - """ - if len(partial) == 2 * n: - result.append(partial) - return - - if open_count < n: - backtrack(partial + "(", open_count + 1, close_count) - - if close_count < open_count: - backtrack(partial + ")", open_count, close_count + 1) - result = [] - backtrack("", 0, 0) + backtrack("", 0, 0, n, result) return result From b9a7acc189528073e23838a3850cdaf53728af40 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:19:47 +0000 Subject: [PATCH 49/51] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/generate_parentheses.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py index 05b4e85cbfd8..8c4df1afefe0 100644 --- a/backtracking/generate_parentheses.py +++ b/backtracking/generate_parentheses.py @@ -32,9 +32,7 @@ def backtrack( """ if len(partial) == 2 * n: # When the combination is complete, add it to the result. - result.append( - partial - ) + result.append(partial) return if open_count < n: From 11dc232656d9a5dfb7f0dfce06543c4cd66560f9 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 13:50:45 +0100 Subject: [PATCH 50/51] Update backtracking/generate_parentheses.py --- backtracking/generate_parentheses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses.py b/backtracking/generate_parentheses.py index 8c4df1afefe0..18c21e2a9b51 100644 --- a/backtracking/generate_parentheses.py +++ b/backtracking/generate_parentheses.py @@ -66,7 +66,7 @@ def generate_parenthesis(n: int) -> list[str]: ['()'] """ - result = [] + result: list[str] = [] backtrack("", 0, 0, n, result) return result From 380f4e49137ea0528e6c2d6ad0c6fda8ec20151a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 13:53:37 +0100 Subject: [PATCH 51/51] Delete matrix/matrix_prefix_sum.py --- matrix/matrix_prefix_sum.py | 86 ------------------------------------- 1 file changed, 86 deletions(-) delete mode 100644 matrix/matrix_prefix_sum.py diff --git a/matrix/matrix_prefix_sum.py b/matrix/matrix_prefix_sum.py deleted file mode 100644 index ada9785b7554..000000000000 --- a/matrix/matrix_prefix_sum.py +++ /dev/null @@ -1,86 +0,0 @@ -""" -Python Program to find prefix sum of a 2D array -""" - - -def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]: - """ - Calculate the prefix sum of a 2D matrix. - Prefix Sum Formula: - prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - - prefix_sum[i - 1][j - 1] + matrix[i][j] - - :param matrix: A 2D matrix. - :return: A matrix containing the prefix sums. - - >>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) - [[1, 2, 3], [2, 4, 6], [3, 6, 9]] - - >>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - [[1, 3, 6], [5, 12, 21], [12, 27, 45]] - """ - rows = len(matrix) - cols = len(matrix[0]) - - # Initialize the prefix sum matrix with zeros, with the - # same dimensions as the original matrix. - prefix_sum = [[0 for _ in range(cols)] for _ in range(rows)] - - # Calculate the prefix sum for the top-left cell. - prefix_sum[0][0] = matrix[0][0] - - # Calculate cumulative sums for the first row. - for i in range(1, cols): - prefix_sum[0][i] = prefix_sum[0][i - 1] + matrix[0][i] - - # Calculate cumulative sums for the first column. - for i in range(1, rows): - prefix_sum[i][0] = prefix_sum[i - 1][0] + matrix[i][0] - - # Update the values in the cells using the general formula. - for i in range(1, rows): - for j in range(1, cols): - # The value in each cell is the sum of: - # - The cell above it - # - The cell to the left of it - # - Subtracting the overlapping cell - # - Adding the value from the original matrix - prefix_sum[i][j] = ( - prefix_sum[i - 1][j] - + prefix_sum[i][j - 1] - - prefix_sum[i - 1][j - 1] - + matrix[i][j] - ) - - return prefix_sum - - -def display_matrix(matrix: list[list[int]]) -> None: - """ - Display a 2D matrix. - - :param matrix: A 2D matrix. - - Display a 2D matrix by printing each row's elements separated by spaces. - - >>> display_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - 1 2 3 - 4 5 6 - 7 8 9 - - >>> display_matrix([[10, 20, 30], [40, 50, 60]]) - 10 20 30 - 40 50 60 - """ - for row in matrix: - # Join the elements of each row with spaces and print the result. - print(" ".join(map(str, row))) - - -if __name__ == "__main__": - matrix = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] - # Calculate the prefix sum of the 2D matrix - prefix_sum_matrix = calculate_prefix_sum(matrix) - - # Display the prefix sum matrix - display_matrix(prefix_sum_matrix)