Create a gist now

Instantly share code, notes, and snippets.

@angch /spiral.py
Last active Aug 29, 2015

What would you like to do?
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),
print
spiral(5, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment