Skip to content
Open
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions data_structures/arrays/spiral_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Solution:
def generate_matrix(self, n: int) -> list[list[int]]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/spiral_matrix.py, please provide doctest for the function generate_matrix

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/spiral_matrix.py, please provide doctest for the function generate_matrix

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/spiral_matrix.py, please provide doctest for the function generate_matrix

Please provide descriptive name for the parameter: n

"""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)

Check failure on line 22 in data_structures/arrays/spiral_matrix.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/spiral_matrix.py:22:1: W293 Blank line contains whitespace
# 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)

Check failure on line 28 in data_structures/arrays/spiral_matrix.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/spiral_matrix.py:28:1: W293 Blank line contains whitespace
# Step 3: Fill the bottom row (right → left)
# Only if there are rows remaining to fill
if row_start <= row_end:
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)

Check failure on line 36 in data_structures/arrays/spiral_matrix.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/spiral_matrix.py:36:1: W293 Blank line contains whitespace
# 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

Check failure on line 43 in data_structures/arrays/spiral_matrix.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/spiral_matrix.py:43:1: W293 Blank line contains whitespace
col_start += 1

# return the completed spiral matrix
return result


# Example usage
if __name__ == "__main__":
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:

Check failure on line 61 in data_structures/arrays/spiral_matrix.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/spiral_matrix.py:61:10: W291 Trailing whitespace
'''
[1, 2, 3]
[8, 9, 4]
[7, 6, 5]

'''
Loading