-
-
Notifications
You must be signed in to change notification settings - Fork 50.1k
Spiral matrix #13523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SubhadipD9
wants to merge
13
commits into
TheAlgorithms:master
Choose a base branch
from
SubhadipD9:spiral-matrix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Spiral matrix #13523
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4fe3771
Add spiral_matrix implementation
SubhadipD9 a387f8e
Adding spiral_matrix implemetation and explain it
SubhadipD9 bdf0001
Adding spiral_matrix code and explanation
SubhadipD9 5ff2bb3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ff14247
fixing the code formating mistake and update the code
SubhadipD9 f3a3cd9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e6f8b27
fixing naming issue
SubhadipD9 b4e0e94
del
SubhadipD9 0ea4a85
Creating code for spiral_matrix DSA problem
SubhadipD9 05a12dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ff578fb
fixing the typo
SubhadipD9 51f3bbb
fix the typo
SubhadipD9 b351094
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide descriptive name for the parameter: |
||
| """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) | ||
|
|
||
| # 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: | ||
| 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) | ||
|
|
||
| # 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 | ||
| 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: | ||
| """ | ||
| [1, 2, 3] | ||
| [8, 9, 4] | ||
| [7, 6, 5] | ||
|
|
||
| """ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 functiongenerate_matrixPlease provide descriptive name for the parameter:
n