-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSpiralPrint.py
78 lines (64 loc) · 1.76 KB
/
SpiralPrint.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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:
# a. First row(left to right)
# b. Last column(top to bottom)
# c. Last row(right to left)
# d. First column(bottom to top)
# Mind that every element will be printed only once.
# Sample Input 1:
# 1
# 4 4
# 1 2 3 4
# 5 6 7 8
# 9 10 11 12
# 13 14 15 16
# Sample Output 1:
# 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
from sys import stdin
def spiralPrint(mat, nRows, mCols):
#Your code goes here
if nRows == 0:
return
r, c = 0, 0
ne = nRows*mCols
count = 0
while count < ne:
i = c
while i < mCols and count < ne:
print(mat[r][i], end=" ")
count = count+1
i = i+1
r = r+1
i = r
while i < nRows and count < ne:
print(mat[i][mCols-1], end=" ")
count = count+1
i = i+1
mCols = mCols-1
i = mCols-1
while i >= c and count < ne:
print(mat[nRows-1][i], end=" ")
count = count+1
i = i-1
nRows = nRows-1
i = nRows-1
while i >= r and count < ne:
print(mat[i][c], end=" ")
count = count+1
i = i-1
c = c+1
#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