↳

def spiral(mat):
mat = np.array(mat)
arr = []
if mat.shape == (1, 1):
arr.append(mat[0][0])
return arr
else:
arr.extend(mat[0, :])
arr.extend(spiral(np.rot90(mat[1:, :])))
return arr Less

↳

def print_spiral(matrix):
print_spiral_help(matrix, 0, 0, 0, len(matrix[0]) - 1, len(matrix) - 1)
def print_spiral_help(matrix, dir, top, left, right, bot):
if left > right or top > bot:
return
# top
if dir == 0:
for i in range(left, right + 1):
print matrix[top][i],
print_spiral_help(matrix, 1, top + 1, left, right, bot)
# Right
elif dir == 1:
for i in range(top, bot + 1):
print matrix[i][right],
print_spiral_help(matrix, 3, top, left, right - 1, bot)
# Left
elif dir == 2:
for i in range(bot, top - 1, -1):
print matrix[i][left],
print_spiral_help(matrix, 0, top, left + 1, right, bot)
# Bottom
elif dir == 3:
for i in range(right, left - 1, -1):
print matrix[bot][i],
print_spiral_help(matrix, 2, top, left, right, bot - 1) Less