Skip to content
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

spiral-matrix #27

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ jobs:
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Test with pytest
run: |
pytest --continue-on-collection-errors
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
git diff origin/main HEAD --name-only -- practice | xargs dirname | sort | uniq | xargs pytest
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
46 changes: 46 additions & 0 deletions practice/spiral-matrix/spiral_matrix.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
def spiral_matrix(size):
matrix = [[0]*size for _ in range(size)]

num = 1
row_start, row_end = 0, size - 1
col_start, col_end = 0, size - 1

while num <= size*size:
# Fill the top row from left to right
for i in range(col_start, col_end + 1):
matrix[row_start][i] = num
num += 1
row_start += 1

# Fill the right column from top to bottom
for i in range(row_start, row_end + 1):
matrix[i][col_end] = num
num += 1
col_end -= 1

# Fill the bottom row from right to left
for i in range(col_end, col_start - 1, -1):
matrix[row_end][i] = num
num += 1
row_end -= 1

# Fill the left column from bottom to top
for i in range(row_end, row_start - 1, -1):
matrix[i][col_start] = num
num += 1
col_start += 1

return matrix
pass

# My Approach ---->

# A while loop is used to fill the matrix in a spiral order.
# The loop continues until num is greater than the total number of elements in the matrix (size * size)

# Inside the loop, four for loops are used to fill
# the top row from left to right,
# the right column from top to bottom,
# the bottom row from right to left,
# and the left column from bottom to top, respectively.
# After each for loop, the corresponding starting or ending index is updated to move towards the center of the matrix.

# After the while loop, the filled matrix is returned.
Loading