Skip to content
Open
Changes from 3 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
65 changes: 65 additions & 0 deletions data_structures/arrays/spiral_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Solution:
def generateMatrix(self, n: int) -> list[list[int]]:

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

data_structures/arrays/spiral_matrix.py:2:9: N802 Function name `generateMatrix` should be lowercase

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 generateMatrix

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: generateMatrix

Please provide descriptive name for the parameter: n

# Create an n x n matrix filled with zeros
result = [[0] * n for _ in range(n)]

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/spiral_matrix.py:5:1: W293 Blank line contains whitespace
# Start filling numbers from 1 to n^2
value = 1

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/spiral_matrix.py:8:1: W293 Blank line contains whitespace
# Define the boundaries for rows and columns
rowStart, rowEnd = 0, n - 1

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/spiral_matrix.py:10:19: N806 Variable `rowEnd` in function should be lowercase

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/spiral_matrix.py:10:9: N806 Variable `rowStart` in function should be lowercase

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: rowStart

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: rowEnd

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: rowStart

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: rowEnd

colStart, colEnd = 0, n - 1

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/spiral_matrix.py:11:19: N806 Variable `colEnd` in function should be lowercase

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/spiral_matrix.py:11:9: N806 Variable `colStart` in function should be lowercase

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: colStart

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: colEnd

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: colStart

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: colEnd


# Continue filling the matrix layer by layer in spiral order
while rowStart <= rowEnd and colStart <= colEnd:

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/spiral_matrix.py:15:1: W293 Blank line contains whitespace
# 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
rowStart += 1 # move top boundary down (row filled)

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/spiral_matrix.py:20:13: N806 Variable `rowStart` in function should be lowercase

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/spiral_matrix.py:21:1: W293 Blank line contains whitespace
# 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:
for k in range(colEnd, colStart - 1, -1):
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)

# Return the completed spiral matrix
return result


# Example usage:
solution = Solution()

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
for row in matrix:
print(row)


# Output:
'''
[1, 2, 3]
[8, 9, 4]
[7, 6, 5]

'''