Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Created March 14, 2020 22:22
Show Gist options
  • Save ahmed4end/51b028e9522ec45cec123bb6f6784912 to your computer and use it in GitHub Desktop.
Save ahmed4end/51b028e9522ec45cec123bb6f6784912 to your computer and use it in GitHub Desktop.
def spiralNumbers(n):
m = [[0]*n for _ in range(n)]
dx, dy = [0,1,0,-1], [1,0,-1,0]
x, y, c = 0,-1,1
for i in range(2*n-1):
for j in range((2*n-i)//2):
x += dx[i%4]
y += dy[i%4]
m[x][y] = c
c+=1
return m
print(spiralNumbers(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment