In below program the output should print the list of integers in spiral of matrix given in program.
Output is : [1, 2, 3, 6, 9, 8, 7, 4, 5, 5]
At the end of the output list the last loop is again printing in reverse order (5
at the end of list is again printing). Why? Where is mistake in my code?
A = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
def spiralOrder(A):
result = []
t = 0
b = len(A)-1
l = 0
r = len(A[0])-1
d = 0
k = 0
while(t<=b and l<=r):
if d == 0:
for i in range(l,r+1):
result.append(A[t][i])
t =+ 1
d = 1
elif d == 1:
for i in range(t,b+1):
result.append(A[i][r])
r -= 1
d = 2
elif d == 2:
for i in range(r,l-1,-1):
result.append(A[b][i])
b -= 1
d = 3
elif d == 3:
for i in range(b,t-1,-1):
result.append(A[i][l])
l += 1
d = 0
return result
print spiralOrder(A)
You are updating
t =+ 1
where you should be doingt += 1
instead. (ind==0
condition).