Created
April 5, 2020 17:21
-
-
Save akayj/203a68370b77f88894fa2a0f3c0a8ed5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment