Skip to content

Instantly share code, notes, and snippets.

@akayj
Created April 5, 2020 17:21
Show Gist options
  • Save akayj/203a68370b77f88894fa2a0f3c0a8ed5 to your computer and use it in GitHub Desktop.
Save akayj/203a68370b77f88894fa2a0f3c0a8ed5 to your computer and use it in GitHub Desktop.
def snake(n):
arr = [[0 for _ in range(n)] for _ in range(n)]
value = 1
border = 0
while value <= n*n:
x, y = border, border
for y in range(border, n-border):
arr[x][y] = value
value += 1
print '(%d,%d) = %2d' % (x, y, arr[x][y])
for x in range(border+1, n-border):
arr[x][y] = value
value += 1
print '(%d,%d) = %2d' % (x, y, arr[x][y])
for y in range(n-border-2, border-1, -1):
arr[x][y] = value
value += 1
print '(%d,%d) = %2d' % (x, y, arr[x][y])
for x in range(n-border-2, border, -1):
arr[x][y] = value
value += 1
print '(%d,%d) = %2d' % (x, y, arr[x][y])
border += 1
print '------------'
return arr
if __name__ == '__main__':
arr = snake(5)
for idx_x, row in enumerate(arr):
for idx_y, col in enumerate(row):
print '(%d,%d) = %2d\t' % (idx_x, idx_y, col),
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment