From 4fe3771d914d72880d2768306fac9e8149f1737b Mon Sep 17 00:00:00 2001 From: Subhadip Bag Date: Thu, 16 Oct 2025 13:38:37 +0530 Subject: [PATCH 01/11] Add spiral_matrix implementation --- data_structures/arrays/spiral_matrix.py | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 data_structures/arrays/spiral_matrix.py diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py new file mode 100644 index 000000000000..e38b57825e62 --- /dev/null +++ b/data_structures/arrays/spiral_matrix.py @@ -0,0 +1,48 @@ +class Solution: + def generateMatrix(self, n: int) -> list[list[int]]: + result = [[0] * n for _ in range(n)] + value = 1 + + colStart, colEnd = 0, n - 1 + rowStart, rowEnd = 0, n - 1 + + while colStart <= colEnd and rowStart <= rowEnd: + # Left to Right + for i in range(colStart, colEnd + 1): + result[rowStart][i] = value + value += 1 + rowStart += 1 + + # Up to Down + for j in range(rowStart, rowEnd + 1): + result[j][colEnd] = value + value += 1 + colEnd -= 1 + + # Right to Left + if rowStart <= rowEnd: + for k in range(colEnd, colStart - 1, -1): + result[rowEnd][k] = value + value += 1 + rowEnd -= 1 + + # Down to Up + if colStart <= colEnd: + for l in range(rowEnd, rowStart - 1, -1): + result[l][colStart] = value + value += 1 + colStart += 1 + + return result + + +# Test +solution = Solution() + +n = 3 + +matrix = solution.generateMatrix(n) + +# Print the matrix +for row in matrix: + print(row) From a387f8e1e7db454d435a912a9be00ec8a3fc24c1 Mon Sep 17 00:00:00 2001 From: Subhadip Bag Date: Thu, 16 Oct 2025 13:41:31 +0530 Subject: [PATCH 02/11] Adding spiral_matrix implemetation and explain it --- data_structures/arrays/spiral_matrix.py | 49 +++++++++++++++++-------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py index e38b57825e62..625b9fe754ec 100644 --- a/data_structures/arrays/spiral_matrix.py +++ b/data_structures/arrays/spiral_matrix.py @@ -1,48 +1,65 @@ class Solution: def generateMatrix(self, n: int) -> list[list[int]]: + # Create an n x n matrix filled with zeros result = [[0] * n for _ in range(n)] + + # Start filling numbers from 1 to n^2 value = 1 - colStart, colEnd = 0, n - 1 + # Define the boundaries for rows and columns rowStart, rowEnd = 0, n - 1 + colStart, colEnd = 0, n - 1 - while colStart <= colEnd and rowStart <= rowEnd: - # Left to Right + # Continue filling the matrix layer by layer in spiral order + while rowStart <= rowEnd and colStart <= colEnd: + + # Step 1: Fill the top row (left → right) for i in range(colStart, colEnd + 1): - result[rowStart][i] = value - value += 1 - rowStart += 1 + result[rowStart][i] = value # assign the current value + value += 1 # move to next number + rowStart += 1 # move top boundary down (row filled) - # Up to Down + # Step 2: Fill the rightmost column (top → bottom) for j in range(rowStart, rowEnd + 1): result[j][colEnd] = value value += 1 - colEnd -= 1 + colEnd -= 1 # move right boundary left (column filled) - # Right to Left + # Step 3: Fill the bottom row (right → left) + # Only if there are rows remaining to fill if rowStart <= rowEnd: for k in range(colEnd, colStart - 1, -1): result[rowEnd][k] = value value += 1 - rowEnd -= 1 + rowEnd -= 1 # move bottom boundary up (row filled) - # Down to Up + # Step 4: Fill the leftmost column (bottom → top) + # Only if there are columns remaining to fill if colStart <= colEnd: for l in range(rowEnd, rowStart - 1, -1): result[l][colStart] = value value += 1 - colStart += 1 + colStart += 1 # move left boundary right (column filled) + # Return the completed spiral matrix return result -# Test +# Example usage: solution = Solution() -n = 3 - +n = 3 # You can change this to any number, e.g. 4 or 5 matrix = solution.generateMatrix(n) -# Print the matrix +# Print the spiral matrix row by row for row in matrix: print(row) + + +# Output: +''' + [1, 2, 3] + [8, 9, 4] + [7, 6, 5] + +''' \ No newline at end of file From bdf00013b85f072cb7c7af5aa8e365f1e3a198aa Mon Sep 17 00:00:00 2001 From: Subhadip Bag Date: Thu, 16 Oct 2025 13:43:31 +0530 Subject: [PATCH 03/11] Adding spiral_matrix code and explanation --- data_structures/arrays/spiral_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py index 625b9fe754ec..0a0846f82eca 100644 --- a/data_structures/arrays/spiral_matrix.py +++ b/data_structures/arrays/spiral_matrix.py @@ -62,4 +62,4 @@ def generateMatrix(self, n: int) -> list[list[int]]: [8, 9, 4] [7, 6, 5] -''' \ No newline at end of file +''' From 5ff2bb32db727d9f4e1a6dd3ffc3e907124848e9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Oct 2025 08:17:40 +0000 Subject: [PATCH 04/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/spiral_matrix.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py index 0a0846f82eca..3f15cd1d1918 100644 --- a/data_structures/arrays/spiral_matrix.py +++ b/data_structures/arrays/spiral_matrix.py @@ -2,29 +2,28 @@ class Solution: def generateMatrix(self, n: int) -> list[list[int]]: # Create an n x n matrix filled with zeros result = [[0] * n for _ in range(n)] - + # Start filling numbers from 1 to n^2 value = 1 - + # Define the boundaries for rows and columns rowStart, rowEnd = 0, n - 1 colStart, colEnd = 0, n - 1 # Continue filling the matrix layer by layer in spiral order while rowStart <= rowEnd and colStart <= colEnd: - # Step 1: Fill the top row (left → right) for i in range(colStart, colEnd + 1): - result[rowStart][i] = value # assign the current value - value += 1 # move to next number + result[rowStart][i] = value # assign the current value + value += 1 # move to next number rowStart += 1 # move top boundary down (row filled) - + # Step 2: Fill the rightmost column (top → bottom) for j in range(rowStart, rowEnd + 1): result[j][colEnd] = value value += 1 colEnd -= 1 # move right boundary left (column filled) - + # Step 3: Fill the bottom row (right → left) # Only if there are rows remaining to fill if rowStart <= rowEnd: @@ -32,7 +31,7 @@ def generateMatrix(self, n: int) -> list[list[int]]: result[rowEnd][k] = value value += 1 rowEnd -= 1 # move bottom boundary up (row filled) - + # Step 4: Fill the leftmost column (bottom → top) # Only if there are columns remaining to fill if colStart <= colEnd: @@ -56,10 +55,10 @@ def generateMatrix(self, n: int) -> list[list[int]]: print(row) -# Output: -''' +# Output: +""" [1, 2, 3] [8, 9, 4] [7, 6, 5] -''' +""" From ff142477511d2f3d242da43510815efb90a29632 Mon Sep 17 00:00:00 2001 From: Subhadip Bag Date: Thu, 16 Oct 2025 13:48:52 +0530 Subject: [PATCH 05/11] fixing the code formating mistake and update the code --- data_structures/arrays/spiral_matrix.py | 38 ++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py index 3f15cd1d1918..d4663b21be81 100644 --- a/data_structures/arrays/spiral_matrix.py +++ b/data_structures/arrays/spiral_matrix.py @@ -1,29 +1,30 @@ class Solution: - def generateMatrix(self, n: int) -> list[list[int]]: - # Create an n x n matrix filled with zeros + def generate_matrix(self, n: int) -> list[list[int]]: + # create an n x n matrix filled with zeros result = [[0] * n for _ in range(n)] - + # Start filling numbers from 1 to n^2 value = 1 - + # Define the boundaries for rows and columns rowStart, rowEnd = 0, n - 1 colStart, colEnd = 0, n - 1 # Continue filling the matrix layer by layer in spiral order while rowStart <= rowEnd and colStart <= colEnd: + # Step 1: Fill the top row (left → right) for i in range(colStart, colEnd + 1): - result[rowStart][i] = value # assign the current value - value += 1 # move to next number + result[rowStart][i] = value # assign the current value + value += 1 # move to next number rowStart += 1 # move top boundary down (row filled) - + # Step 2: Fill the rightmost column (top → bottom) for j in range(rowStart, rowEnd + 1): result[j][colEnd] = value value += 1 colEnd -= 1 # move right boundary left (column filled) - + # Step 3: Fill the bottom row (right → left) # Only if there are rows remaining to fill if rowStart <= rowEnd: @@ -31,34 +32,33 @@ def generateMatrix(self, n: int) -> list[list[int]]: result[rowEnd][k] = value value += 1 rowEnd -= 1 # move bottom boundary up (row filled) - + # Step 4: Fill the leftmost column (bottom → top) # Only if there are columns remaining to fill if colStart <= colEnd: for l in range(rowEnd, rowStart - 1, -1): result[l][colStart] = value value += 1 - colStart += 1 # move left boundary right (column filled) + col_start += 1 - # Return the completed spiral matrix + # return the completed spiral matrix return result -# Example usage: +# example usage solution = Solution() +n = 3 # change this to any number, e.g., 4 or 5 +matrix = solution.generate_matrix(n) -n = 3 # You can change this to any number, e.g. 4 or 5 -matrix = solution.generateMatrix(n) - -# Print the spiral matrix row by row +# print the spiral matrix row by row for row in matrix: print(row) -# Output: -""" +# Output: +''' [1, 2, 3] [8, 9, 4] [7, 6, 5] -""" +''' From f3a3cd9b5abebbc0a23beb46b30c8e41dfe7d775 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Oct 2025 08:22:40 +0000 Subject: [PATCH 06/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/spiral_matrix.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py index d4663b21be81..98b4ecec10b9 100644 --- a/data_structures/arrays/spiral_matrix.py +++ b/data_structures/arrays/spiral_matrix.py @@ -2,29 +2,28 @@ class Solution: def generate_matrix(self, n: int) -> list[list[int]]: # create an n x n matrix filled with zeros result = [[0] * n for _ in range(n)] - + # Start filling numbers from 1 to n^2 value = 1 - + # Define the boundaries for rows and columns rowStart, rowEnd = 0, n - 1 colStart, colEnd = 0, n - 1 # Continue filling the matrix layer by layer in spiral order while rowStart <= rowEnd and colStart <= colEnd: - # Step 1: Fill the top row (left → right) for i in range(colStart, colEnd + 1): - result[rowStart][i] = value # assign the current value - value += 1 # move to next number + result[rowStart][i] = value # assign the current value + value += 1 # move to next number rowStart += 1 # move top boundary down (row filled) - + # Step 2: Fill the rightmost column (top → bottom) for j in range(rowStart, rowEnd + 1): result[j][colEnd] = value value += 1 colEnd -= 1 # move right boundary left (column filled) - + # Step 3: Fill the bottom row (right → left) # Only if there are rows remaining to fill if rowStart <= rowEnd: @@ -32,7 +31,7 @@ def generate_matrix(self, n: int) -> list[list[int]]: result[rowEnd][k] = value value += 1 rowEnd -= 1 # move bottom boundary up (row filled) - + # Step 4: Fill the leftmost column (bottom → top) # Only if there are columns remaining to fill if colStart <= colEnd: @@ -55,10 +54,10 @@ def generate_matrix(self, n: int) -> list[list[int]]: print(row) -# Output: -''' +# Output: +""" [1, 2, 3] [8, 9, 4] [7, 6, 5] -''' +""" From e6f8b2793eab4065adaf96679654929407fdf860 Mon Sep 17 00:00:00 2001 From: Subhadip Bag Date: Thu, 16 Oct 2025 13:55:20 +0530 Subject: [PATCH 07/11] fixing naming issue --- data_structures/arrays/spiral_matrix.py | 33 +++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py index d4663b21be81..65cf6fe4dcc7 100644 --- a/data_structures/arrays/spiral_matrix.py +++ b/data_structures/arrays/spiral_matrix.py @@ -7,38 +7,39 @@ def generate_matrix(self, n: int) -> list[list[int]]: value = 1 # Define the boundaries for rows and columns - rowStart, rowEnd = 0, n - 1 - colStart, colEnd = 0, n - 1 + row_start, row_end = 0, n - 1 + col_start, col_end = 0, n - 1 # Continue filling the matrix layer by layer in spiral order - while rowStart <= rowEnd and colStart <= colEnd: + while row_start <= row_end and col_start <= col_end: # Step 1: Fill the top row (left → right) - for i in range(colStart, colEnd + 1): - result[rowStart][i] = value # assign the current value + for i in range(col_start, col_end + 1): + result[row_start][i] = value # assign the current value value += 1 # move to next number - rowStart += 1 # move top boundary down (row filled) + row_start += 1 # move top boundary down (row filled) # Step 2: Fill the rightmost column (top → bottom) - for j in range(rowStart, rowEnd + 1): - result[j][colEnd] = value + for j in range(row_start, row_end + 1): + result[j][col_end] = value value += 1 - colEnd -= 1 # move right boundary left (column filled) + col_end -= 1 # move right boundary left (column filled) # Step 3: Fill the bottom row (right → left) # Only if there are rows remaining to fill - if rowStart <= rowEnd: - for k in range(colEnd, colStart - 1, -1): - result[rowEnd][k] = value + if row_start <= row_end: + for k in range(col_end, col_start - 1, -1): + result[row_end][k] = value value += 1 - rowEnd -= 1 # move bottom boundary up (row filled) + row_end -= 1 # move bottom boundary up (row filled) # Step 4: Fill the leftmost column (bottom → top) # Only if there are columns remaining to fill - if colStart <= colEnd: - for l in range(rowEnd, rowStart - 1, -1): - result[l][colStart] = value + if col_start <= col_end: + for l in range(row_end, row_start - 1, -1): + result[l][col_start] = value value += 1 + col_start += 1 # return the completed spiral matrix From 0ea4a85dba6770cd8fee530002e4c925be663ac7 Mon Sep 17 00:00:00 2001 From: Subhadip Bag Date: Thu, 16 Oct 2025 14:01:02 +0530 Subject: [PATCH 08/11] Creating code for spiral_matrix DSA problem --- data_structures/arrays/spiral_matrix.py | 65 +++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 data_structures/arrays/spiral_matrix.py diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py new file mode 100644 index 000000000000..65cf6fe4dcc7 --- /dev/null +++ b/data_structures/arrays/spiral_matrix.py @@ -0,0 +1,65 @@ +class Solution: + def generate_matrix(self, n: int) -> list[list[int]]: + # create an n x n matrix filled with zeros + result = [[0] * n for _ in range(n)] + + # Start filling numbers from 1 to n^2 + value = 1 + + # Define the boundaries for rows and columns + row_start, row_end = 0, n - 1 + col_start, col_end = 0, n - 1 + + # Continue filling the matrix layer by layer in spiral order + while row_start <= row_end and col_start <= col_end: + + # Step 1: Fill the top row (left → right) + for i in range(col_start, col_end + 1): + result[row_start][i] = value # assign the current value + value += 1 # move to next number + row_start += 1 # move top boundary down (row filled) + + # Step 2: Fill the rightmost column (top → bottom) + for j in range(row_start, row_end + 1): + result[j][col_end] = value + value += 1 + col_end -= 1 # move right boundary left (column filled) + + # Step 3: Fill the bottom row (right → left) + # Only if there are rows remaining to fill + if row_start <= row_end: + for k in range(col_end, col_start - 1, -1): + result[row_end][k] = value + value += 1 + row_end -= 1 # move bottom boundary up (row filled) + + # Step 4: Fill the leftmost column (bottom → top) + # Only if there are columns remaining to fill + if col_start <= col_end: + for l in range(row_end, row_start - 1, -1): + result[l][col_start] = value + value += 1 + + col_start += 1 + + # return the completed spiral matrix + return result + + +# example usage +solution = Solution() +n = 3 # change this to any number, e.g., 4 or 5 +matrix = solution.generate_matrix(n) + +# print the spiral matrix row by row +for row in matrix: + print(row) + + +# Output: +''' + [1, 2, 3] + [8, 9, 4] + [7, 6, 5] + +''' From 05a12dc86778e92474b6b3d57cfb9b8a1326c642 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Oct 2025 08:31:37 +0000 Subject: [PATCH 09/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/spiral_matrix.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py index 65cf6fe4dcc7..e52e03c498b9 100644 --- a/data_structures/arrays/spiral_matrix.py +++ b/data_structures/arrays/spiral_matrix.py @@ -2,29 +2,28 @@ class Solution: def generate_matrix(self, n: int) -> list[list[int]]: # create an n x n matrix filled with zeros result = [[0] * n for _ in range(n)] - + # Start filling numbers from 1 to n^2 value = 1 - + # Define the boundaries for rows and columns row_start, row_end = 0, n - 1 col_start, col_end = 0, n - 1 # Continue filling the matrix layer by layer in spiral order while row_start <= row_end and col_start <= col_end: - # Step 1: Fill the top row (left → right) for i in range(col_start, col_end + 1): - result[row_start][i] = value # assign the current value - value += 1 # move to next number + result[row_start][i] = value # assign the current value + value += 1 # move to next number row_start += 1 # move top boundary down (row filled) - + # Step 2: Fill the rightmost column (top → bottom) for j in range(row_start, row_end + 1): result[j][col_end] = value value += 1 col_end -= 1 # move right boundary left (column filled) - + # Step 3: Fill the bottom row (right → left) # Only if there are rows remaining to fill if row_start <= row_end: @@ -32,14 +31,14 @@ def generate_matrix(self, n: int) -> list[list[int]]: result[row_end][k] = value value += 1 row_end -= 1 # move bottom boundary up (row filled) - + # Step 4: Fill the leftmost column (bottom → top) # Only if there are columns remaining to fill if col_start <= col_end: for l in range(row_end, row_start - 1, -1): result[l][col_start] = value value += 1 - + col_start += 1 # return the completed spiral matrix @@ -56,10 +55,10 @@ def generate_matrix(self, n: int) -> list[list[int]]: print(row) -# Output: -''' +# Output: +""" [1, 2, 3] [8, 9, 4] [7, 6, 5] -''' +""" From ff578fb1f74ab7e600e75a8cc96bce4bf65a8d7d Mon Sep 17 00:00:00 2001 From: Subhadip Bag Date: Thu, 16 Oct 2025 14:06:01 +0530 Subject: [PATCH 10/11] fixing the typo --- data_structures/arrays/spiral_matrix.py | 71 ++++++++++++------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py index 65cf6fe4dcc7..c957cded5c8a 100644 --- a/data_structures/arrays/spiral_matrix.py +++ b/data_structures/arrays/spiral_matrix.py @@ -1,65 +1,62 @@ class Solution: def generate_matrix(self, n: int) -> list[list[int]]: - # create an n x n matrix filled with zeros + """Generate an n x n spiral matrix filled with numbers from 1 to n^2.""" + + # Create an n x n matrix filled with zeros result = [[0] * n for _ in range(n)] - + # Start filling numbers from 1 to n^2 value = 1 - + # Define the boundaries for rows and columns row_start, row_end = 0, n - 1 col_start, col_end = 0, n - 1 # Continue filling the matrix layer by layer in spiral order while row_start <= row_end and col_start <= col_end: - # Step 1: Fill the top row (left → right) - for i in range(col_start, col_end + 1): - result[row_start][i] = value # assign the current value - value += 1 # move to next number - row_start += 1 # move top boundary down (row filled) - + for col in range(col_start, col_end + 1): + result[row_start][col] = value # Assign the current value + value += 1 # Move to next number + row_start += 1 # Move top boundary down (row filled) + # Step 2: Fill the rightmost column (top → bottom) - for j in range(row_start, row_end + 1): - result[j][col_end] = value + for row in range(row_start, row_end + 1): + result[row][col_end] = value value += 1 - col_end -= 1 # move right boundary left (column filled) - + col_end -= 1 # Move right boundary left (column filled) + # Step 3: Fill the bottom row (right → left) # Only if there are rows remaining to fill if row_start <= row_end: - for k in range(col_end, col_start - 1, -1): - result[row_end][k] = value + for col in range(col_end, col_start - 1, -1): + result[row_end][col] = value value += 1 - row_end -= 1 # move bottom boundary up (row filled) - + row_end -= 1 # Move bottom boundary up (row filled) + # Step 4: Fill the leftmost column (bottom → top) # Only if there are columns remaining to fill if col_start <= col_end: - for l in range(row_end, row_start - 1, -1): - result[l][col_start] = value + for row in range(row_end, row_start - 1, -1): + result[row][col_start] = value value += 1 - - col_start += 1 + col_start += 1 # Move left boundary right (column filled) - # return the completed spiral matrix + # Return the completed spiral matrix return result -# example usage -solution = Solution() -n = 3 # change this to any number, e.g., 4 or 5 -matrix = solution.generate_matrix(n) - -# print the spiral matrix row by row -for row in matrix: - print(row) - +# Example usage +if __name__ == "__main__": + solution = Solution() + n = 3 # Change this to any number, e.g., 4 or 5 + matrix = solution.generate_matrix(n) -# Output: -''' - [1, 2, 3] - [8, 9, 4] - [7, 6, 5] + # Print the spiral matrix row by row + for row in matrix: + print(row) -''' + # Output: + # [1, 2, 3] + # [8, 9, 4] + # [7, 6, 5] From b3510945848ee4e4655ed4f32a2225f0472ba6ab Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Oct 2025 08:37:53 +0000 Subject: [PATCH 11/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/spiral_matrix.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/data_structures/arrays/spiral_matrix.py b/data_structures/arrays/spiral_matrix.py index 7d5e692bb6d6..e1f80372b5de 100644 --- a/data_structures/arrays/spiral_matrix.py +++ b/data_structures/arrays/spiral_matrix.py @@ -16,16 +16,16 @@ def generate_matrix(self, n: int) -> list[list[int]]: while row_start <= row_end and col_start <= col_end: # Step 1: Fill the top row (left → right) for i in range(col_start, col_end + 1): - result[row_start][i] = value # assign the current value - value += 1 # move to next number + result[row_start][i] = value # assign the current value + value += 1 # move to next number row_start += 1 # move top boundary down (row filled) - + # Step 2: Fill the rightmost column (top → bottom) for row in range(row_start, row_end + 1): result[row][col_end] = value value += 1 col_end -= 1 # move right boundary left (column filled) - + # Step 3: Fill the bottom row (right → left) # Only if there are rows remaining to fill if row_start <= row_end: @@ -33,14 +33,14 @@ def generate_matrix(self, n: int) -> list[list[int]]: result[row_end][col] = value value += 1 row_end -= 1 # move bottom boundary up (row filled) - + # Step 4: Fill the leftmost column (bottom → top) # Only if there are columns remaining to fill if col_start <= col_end: for row in range(row_end, row_start - 1, -1): result[row][col_start] = value value += 1 - + col_start += 1 # return the completed spiral matrix @@ -58,10 +58,10 @@ def generate_matrix(self, n: int) -> list[list[int]]: print(row) -# Output: -''' +# Output: +""" [1, 2, 3] [8, 9, 4] [7, 6, 5] -''' +"""