Skip to content

Commit 5bc513f

Browse files
Merge pull request geekcomputers#314 from Naman-Garg-06/patch-1
spiralmatrix.py
2 parents 74da727 + 84d6c89 commit 5bc513f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

spiralmatrix.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
n=int(input("Enter the size of matrix:"))
2+
t=1
3+
r=0 # r stands for row
4+
c=0 # c stands for column
5+
matrix=[[0 for x in range(n)]for y in range(n)] # to initialise the matrix
6+
if n%2==0:
7+
k=n//2
8+
else:
9+
k=int((n/2)+1)
10+
for i in range(k):
11+
while c<n:
12+
matrix[r][c]=t
13+
t=t+1
14+
c=c+1
15+
r=r+1
16+
c=c-1
17+
while r<n:
18+
matrix[r][c]=t
19+
t=t+1
20+
r=r+1
21+
r=r-1
22+
c=c-1
23+
while c>=i:
24+
matrix[r][c]=t
25+
c=c-1
26+
t=t+1
27+
c=c+1
28+
r=r-1
29+
while r>i:
30+
matrix[r][c]=t
31+
t=t+1
32+
r=r-1
33+
r=r+1
34+
n=n-1
35+
c=c+1
36+
for m in matrix:
37+
print(m)

0 commit comments

Comments
 (0)