Last active
August 29, 2015 14:06
-
-
Save angch/f4400ec149471eb2bd2a 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 spiral(max_x, max_y): | |
matrix = [] | |
for i in range(0, max_y): | |
matrix.append([0] * max_x) | |
x, y, n, xdir, ydir = 0, 0, 0, 1, 0 | |
while 1: | |
n = n + 1 | |
matrix[y][x] = n | |
x, y = x + xdir, y + ydir | |
if (x < 0 or x >= max_x or y < 0 or y >= max_y or matrix[y][x] > 0): | |
x, y = x - xdir, y - ydir | |
xdir, ydir = -ydir, xdir | |
x, y = x + xdir, y + ydir | |
if (matrix[y][x] > 0): | |
break | |
for i in matrix: | |
for j in i: | |
print '{0:2}'.format(j), | |
spiral(5, 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment