Skip to content

Commit

Permalink
Added printing Matrix in Spiral geekquad#20
Browse files Browse the repository at this point in the history
  • Loading branch information
kritikaparmar-programmer committed Oct 1, 2020
1 parent 87b3e18 commit b5d44f2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions python/Matrix/Spiral_Print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
def Spiralprint(matrix):

top = left = 0 # initializing with top
bottom = len(matrix) - 1
right = len(matrix[0]) - 1

while True:
if left > right:
break

# print top row
for i in range(left, right + 1):
print(matrix[top][i], end=' ')
top = top + 1

if top > bottom:
break

# print right column
for i in range(top, bottom + 1):
print(matrix[i][right], end=' ')
right = right - 1

if left > right:
break

# print bottom row
for i in range(right, left - 1, -1):
print(matrix[bottom][i], end=' ')
bottom = bottom - 1

if top > bottom:
break

# print left column
for i in range(bottom, top - 1, -1):
print(matrix[i][left], end=' ')
left = left + 1


rows = int(input())
cols = int(input())
matrix = []
for i in range(0, rows):
arr = list(map(int, input().split()[:cols]))
matrix.append(arr)

Spiralprint(matrix)

0 comments on commit b5d44f2

Please sign in to comment.