Skip to content

Instantly share code, notes, and snippets.

@atul-chaudhary
Created September 8, 2019 03:07
Show Gist options
  • Save atul-chaudhary/e43a2d7da193287d5d43378c9a942b31 to your computer and use it in GitHub Desktop.
Save atul-chaudhary/e43a2d7da193287d5d43378c9a942b31 to your computer and use it in GitHub Desktop.
Print matrix in spiral
a = [[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18]]
int_row = 0
last_row = 3
int_col = 0
last_col = 6
while(int_row<=last_row and int_col<=last_col):
for i in range(int_col,last_col):
print(a[int_row][i],end=' ')
int_row+=1
for j in range(int_row,last_row):
print(a[j][last_col-1],end=' ')
last_col-=1
if(int_row<=last_row):
for i in range(last_col-1,-1,-1):
print(a[last_row-1][i],end=' ')
last_row-=1
if(int_col<=last_col):
for j in range(last_row-1,0,-1):
print(a[j][int_col],end=' ')
int_col+=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment