Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def spiral_form(array):
- if not array:
- return
- n = len(array)
- m = len(array[0])
- result = []
- left = 0 # start column index
- right = m - 1 # end column index
- up = 0 # start row index
- down = n - 1 # end row index
- while len(result) < n * m:
- for i in range(left, right + 1):
- result.append(array[left][i])
- for i in range(up + 1, down + 1):
- result.append(array[i][right])
- if len(result) < n * m:
- for i in range(right - 1, left - 1, -1):
- result.append(array[down][i])
- for i in range(down - 1, up, -1):
- result.append(array[i][left])
- left += 1
- right -= 1
- down -= 1
- up += 1
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement