From 0989a2c86ecb9e4cedc5d4ce7d5c5dcba9448f5a Mon Sep 17 00:00:00 2001 From: anubhav-qt Date: Thu, 3 Oct 2024 20:57:50 +0530 Subject: [PATCH 1/5] Add diagonal matrix traversal algorithm --- matrix/diagonal_traversal.py | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 matrix/diagonal_traversal.py diff --git a/matrix/diagonal_traversal.py b/matrix/diagonal_traversal.py new file mode 100644 index 000000000000..bc8702b7535e --- /dev/null +++ b/matrix/diagonal_traversal.py @@ -0,0 +1,67 @@ +from typing import List + +def find_diagonal_order(mat: List[List[int]]) -> List[int]: + """ + Returns the elements of the matrix in diagonal order. + + :param mat: A 2D list representing the matrix. + :return: A list of elements in diagonal order. + :raises ValueError: If the matrix is empty or rows have different lengths. + + >>> find_diagonal_order([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + [1, 2, 4, 7, 5, 3, 6, 8, 9] + + Example: + matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + Algorithm: + Step 1. Initialize variables to track the current row and column. + Step 2. Iterate through the elements of the matrix in a + manner that alternates between moving diagonally up + and down. + """ + if not mat or not mat[0]: + return [] + + m, n = len(mat), len(mat[0]) + + # Check for uniformity of row lengths + for row in mat: + if len(row) != n: + raise ValueError("All rows must have the same number of columns.") + + result = [] + row, col = 0, 0 + + for _ in range(m * n): + result.append(mat[row][col]) + + # Determine the direction of traversal + if (row + col) % 2 == 0: # moving up + if col == n - 1: # hit the right boundary + row += 1 + elif row == 0: # hit the top boundary + col += 1 + else: # move up diagonally + row -= 1 + col += 1 + else: # moving down + if row == m - 1: # hit the bottom boundary + col += 1 + elif col == 0: # hit the left boundary + row += 1 + else: # move down diagonally + row += 1 + col -= 1 + + return result + + +# Driver code +if __name__ == "__main__": + import doctest + + doctest.testmod() + + # Example usage + matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + print(find_diagonal_order(matrix)) From 9898007442525c4307a89041af52cccca1d670c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 15:35:30 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/diagonal_traversal.py | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix/diagonal_traversal.py b/matrix/diagonal_traversal.py index bc8702b7535e..418505689b9a 100644 --- a/matrix/diagonal_traversal.py +++ b/matrix/diagonal_traversal.py @@ -1,5 +1,6 @@ from typing import List + def find_diagonal_order(mat: List[List[int]]) -> List[int]: """ Returns the elements of the matrix in diagonal order. From ba3cf0aa0065f60e86035d9fdb52c3f218de2008 Mon Sep 17 00:00:00 2001 From: Anubhav Joshi <48016317+anubhav-qt@users.noreply.github.com> Date: Thu, 3 Oct 2024 21:14:11 +0530 Subject: [PATCH 3/5] Update diagonal_traversal.py to pass ruff test --- matrix/diagonal_traversal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matrix/diagonal_traversal.py b/matrix/diagonal_traversal.py index 418505689b9a..b44447e12c13 100644 --- a/matrix/diagonal_traversal.py +++ b/matrix/diagonal_traversal.py @@ -1,7 +1,7 @@ -from typing import List +from typing import list -def find_diagonal_order(mat: List[List[int]]) -> List[int]: +def find_diagonal_order(mat: list[list[int]]) -> list[int]: """ Returns the elements of the matrix in diagonal order. From 204c38836bcb538f40d78fdb8ba0dc61e2c62100 Mon Sep 17 00:00:00 2001 From: Anubhav Joshi <48016317+anubhav-qt@users.noreply.github.com> Date: Sat, 5 Oct 2024 19:12:43 +0530 Subject: [PATCH 4/5] Update diagonal_traversal.py to adhere ruff suggestions --- matrix/diagonal_traversal.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/matrix/diagonal_traversal.py b/matrix/diagonal_traversal.py index b44447e12c13..88bd28d39333 100644 --- a/matrix/diagonal_traversal.py +++ b/matrix/diagonal_traversal.py @@ -45,14 +45,14 @@ def find_diagonal_order(mat: list[list[int]]) -> list[int]: else: # move up diagonally row -= 1 col += 1 - else: # moving down - if row == m - 1: # hit the bottom boundary - col += 1 - elif col == 0: # hit the left boundary - row += 1 - else: # move down diagonally - row += 1 - col -= 1 + elif row == m - 1: # hit the bottom boundary + col += 1 + elif col == 0: # hit the left boundary + row += 1 + elif col > 0 and row < m - 1: # move down diagonally + row += 1 + col -= 1 + return result From 621a09f07446bbc7bd54d682dce1be512080ba3d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2024 13:45:05 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/diagonal_traversal.py | 1 - 1 file changed, 1 deletion(-) diff --git a/matrix/diagonal_traversal.py b/matrix/diagonal_traversal.py index 88bd28d39333..2ed9f3d7c712 100644 --- a/matrix/diagonal_traversal.py +++ b/matrix/diagonal_traversal.py @@ -53,7 +53,6 @@ def find_diagonal_order(mat: list[list[int]]) -> list[int]: row += 1 col -= 1 - return result