-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSpiral Print.py
45 lines (33 loc) · 1.08 KB
/
Spiral Print.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# For a given two-dimensional integer array/list of size (N x M), print it in a spiral form.
# That is, you need to print in the order followed for every iteration:
from sys import stdin
def spiralPrint(mat, nRows, mCols):
#Your code goes here
loop = nRows
if loop % 2 != 0:
loop += 1
for i in range(loop//2):
for j in range(i, mCols-i):
print(mat[i][j], end=" ")
for k in range(1+i, nRows-i):
print(mat[k][j], end=" ")
for l in range(mCols-2-i, -1+i, -1):
print(mat[k][l], end=" ")
for p in range(nRows-2-i, 0+i, -1):
print(mat[p][l], end=" ")
#Taking Input Using Fast I/O
def take2DInput():
li = stdin.readline().rstrip().split(" ")
nRows = int(li[0])
mCols = int(li[1])
if nRows == 0:
return list(), 0, 0
mat = [list(map(int, input().strip().split(" "))) for row in range(nRows)]
return mat, nRows, mCols
#main
t = int(stdin.readline().rstrip())
while t > 0:
mat, nRows, mCols = take2DInput()
spiralPrint(mat, nRows, mCols)
print()
t -= 1