Skip to content

Instantly share code, notes, and snippets.

@jeb2239
Created November 12, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeb2239/4f3d28d26332bee2f5e53973a8696d63 to your computer and use it in GitHub Desktop.
Save jeb2239/4f3d28d26332bee2f5e53973a8696d63 to your computer and use it in GitHub Desktop.
UP=3
LEFT=2
DOWN=1
RIGHT=0
DIRS = [(0,1),(1,0),(0,-1),(-1,0)]
def spiral_copy(inputMatrix):
n=len(inputMatrix)
m=len(inputMatrix[0])
curr_dir=RIGHT
r=0
c=0
result=[]
for i in range(m*n):
print(r,c)
print(inputMatrix[r][c])
result.append(inputMatrix[r][c])
inputMatrix[r][c]=None
new_r = r + DIRS[curr_dir][0]
new_c = c + DIRS[curr_dir][1]
not_valid=not ((0 <= new_r <m and 0<= new_c<n) and (inputMatrix[new_r][new_c] is None))
if not_valid:
curr_dir=(curr_dir+1)%4
new_r = r+DIRS[curr_dir][0]
new_c = c+DIRS[curr_dir][1]
is_visited=(inputMatrix[new_r][new_c] is None)
print(curr_dir,'---')
r=new_r
c=new_c
print(inputMatrix)
return result
print(spiral_copy([ [1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20] ]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment