I'm looking to print a matrix in spiral way but I'm having some issues understanding when/how exactly to change the boolean values to make it work. This is what I have till now:
A 4x5 matrix
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
and the function that should print it
def print_spiral(matrix): left = False right = True down = False up = False min_x = 0 max_x = len(matrix) -1 min_y = 0 max_y = len (matrix[0]) - 1 x,y = 0, 0 while True: print matrix[x][y] if y < max_y and x == min_x: y=y+1 elif y == max_y and x < max_x: x=x+1 elif x == max_x and y > min_y: right = False y=y-1 elif y == min_y and x > min_x: x = x-1
As it is now, is not printing the matrix in a spiral way but is going in a loop printing the outside rows and columns of the matrix. I'm trying to solve this problem by making use of the boolean left, right, up , down variables but I'm stuck with the logic at this point. Any advices please?!